Author Topic: UIWidget 'auto-adjust to match' size for BoxCollider2D  (Read 3652 times)

junkier

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
UIWidget 'auto-adjust to match' size for BoxCollider2D
« on: May 21, 2014, 11:57:42 AM »
Hi!

I tried to search this question but no results...

So, I can't find checkbox in UIWidget's inspector window, that will auto-resize attached BoxCollider2D automatically.

And as I understand, I'm not allowed to mix BoxColliders & BoxCollider2Ds under one UICamera on one Layer (for properly touches handling),
so I need to use only BoxCollider2D if I switch UICamera to Unity2D Event Type.

So is 'auto-adjust' for BoxCollider2D feature scheduled for furher updates? Or I need to use some workarounds or hacks?
Thank you!


Edit:
As for now, I found 'hack' solution:
1) Add UIWidget and BoxCollider components to some GameObject;
2) In UIWidget's inspector window will apear 'auto-adjust to match' checkbox. Set it to Checked.
3) Remove BoxCollider and then add BoxCollider2D.
4) Then add some code to UIWidget.cs:

  1. public void ResizeCollider ()
  2. {
  3.     if (NGUITools.GetActive(this))
  4.     {
  5.         BoxCollider box = collider as BoxCollider;
  6.         if (box != null) NGUITools.UpdateWidgetCollider(box, true);
  7.        
  8.                 // ** Modified for BoxCollider2D
  9.                 else
  10.                 {
  11.                     var box2D = collider2D as BoxCollider2D;
  12.             if (box2D == null)
  13.                 return;
  14.             Vector3[] corners = localCorners;
  15.             box2D.center = Vector3.Lerp(corners[0], corners[2], 0.5f);
  16.             box2D.size = corners[2] - corners[0];
  17.                 }
  18.         // **
  19.     }
  20. }

5) Now our BoxCollider2D appears to be updated when UIWidget's dimensions change.
« Last Edit: May 21, 2014, 12:16:36 PM by junkier »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIWidget 'auto-adjust to match' size for BoxCollider2D
« Reply #1 on: May 21, 2014, 12:53:08 PM »
Next update adds native support for 2D colliders, and if your camera is set to 2D UI event type, NGUI will actually be creating 2D colliders instead of 3D. So... next update onwards your hack won't be needed. :)
« Last Edit: May 21, 2014, 01:38:15 PM by ArenMook »