Author Topic: NGUI Interactions not working with new Unity 2D Colliders  (Read 10602 times)

joshsplosion

  • Guest
NGUI Interactions not working with new Unity 2D Colliders
« on: November 17, 2013, 08:12:39 PM »
Unity 4.3.0f4
NGUI 3.0.5


I have two cubes that are duplicates of each other in every way except one has a normal 3D box collider, the other has a new 2D box collider.

Both respond to the Monobehavior's OnMouseDown function, but the 2D box collider does NOT register normal raycasting. This seems to be stopping NGUI's interactions from registering on objects with 2D colliders.

Are there any solutions to this, or is it too soon?

Edit: From what I've found, the normal raycasting is completely separate because it is part of the 3D system which doesnt interact with the 2D system. Still wondering how NGUI is going to deal with that.
« Last Edit: November 17, 2013, 08:17:56 PM by joshsplosion »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Interactions not working with new Unity 2D Colliders
« Reply #1 on: November 18, 2013, 01:20:38 AM »
Instead of Physics.Raycast performed by the UICamera, Physics2D.Raycast would have to be used. You could search for Physics.Raycast in UICamera and change it yourself, if you were so inclined -- but I can't simply swap from one to the other because doing so will break backwards compatibility.

loopyllama

  • Guest
Re: NGUI Interactions not working with new Unity 2D Colliders
« Reply #2 on: December 02, 2013, 05:33:27 AM »
I wanted to use NGUI's touch messaging, so I had to do this. it wasn't bad.
all of the changes are in UICamera. Change all of the Physics to Physics2D and the RaycastHit to RaycastHit2D.

finally the 2d raycast cannot provide an "out" ray as an argument like the 3d raycast, and it does not return a bool, so there is a slight logic change...

what used to look like:
if ((Physics.Raycast (ray, out hit, dist, mask))

now looks like:
hit = Physics2D.Raycast (ray.origin, ray.direction, dist, mask);
if (hit.collider != null)

(don't forget you have to change all of the RaycastHit to RaycastHit2D and Physics to Physics2D, so that hit is actually a RaycastHit2D).

there is also a similar change with the whole:
RaycastHit[] hits = Physics.RaycastAll(...

becoming:
RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction, ...

these changes basically make NGUI only detect hits with 2d colliders and not with 3d colliders. I plan on making my UI widgets, then manually changing the rigidbody to rigidbody2d and the collider to collider2d on all of the panels and widgets.

junkier

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: NGUI Interactions not working with new Unity 2D Colliders
« Reply #3 on: May 20, 2014, 04:49:58 AM »
I wanted to use NGUI's touch messaging, so I had to do this. it wasn't bad.
all of the changes are in UICamera. Change all of the Physics to Physics2D and the RaycastHit to RaycastHit2D.

finally the 2d raycast cannot provide an "out" ray as an argument like the 3d raycast, and it does not return a bool, so there is a slight logic change...

what used to look like:
if ((Physics.Raycast (ray, out hit, dist, mask))

now looks like:
hit = Physics2D.Raycast (ray.origin, ray.direction, dist, mask);
if (hit.collider != null)

(don't forget you have to change all of the RaycastHit to RaycastHit2D and Physics to Physics2D, so that hit is actually a RaycastHit2D).

there is also a similar change with the whole:
RaycastHit[] hits = Physics.RaycastAll(...

becoming:
RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction, ...

these changes basically make NGUI only detect hits with 2d colliders and not with 3d colliders. I plan on making my UI widgets, then manually changing the rigidbody to rigidbody2d and the collider to collider2d on all of the panels and widgets.


loopyllama, thank you! I will try to add your changes and do not remove 3D colliders support.
I think it would be great to make support for both 2D and 3D types of Colliders in NGUI!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Interactions not working with new Unity 2D Colliders
« Reply #4 on: May 20, 2014, 01:55:49 PM »
This is already supported by NGUI natively. Change the event type on the UICamera.

junkier

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: NGUI Interactions not working with new Unity 2D Colliders
« Reply #5 on: May 21, 2014, 11:26:31 AM »
This is already supported by NGUI natively. Change the event type on the UICamera.

Wow... I'm confused... It seems I've missed it in versions notes. Thanks!