Author Topic: Nested Widgets and RayCasts  (Read 3108 times)

YourUncleBob

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Nested Widgets and RayCasts
« on: November 13, 2013, 12:32:20 PM »
I've run into a problem with nested widgets and raycasts. In many of our scenes, we have widgets with collisions on them nested below other widgets with collisions on them. In these cases, the widget that gets button events is unpredictable.

Looking at the code, CalculateRaycastDepth (I'm currently on 3.0.2 which has CalculateSortingDepth) uses GetComponentsInChildren<UIWidget> to collect all widgets beneath the GameObject that was collided with.  3.0.2 returns the Max depth of any of those widgets, 3.0.5 returns the Min depth. Neither seem like the correct way to go in all cases.

If the collision being considered is actually on a widget, I think you'd want that widget's depth. It gets really complicated if a non-widget attached collision has collisions on children.

I added:
UIWidget w = go.GetComponent<UIWidget>();
if(w != null)
{
return(w.sortingDepth + 1);
      }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Nested Widgets and RayCasts
« Reply #1 on: November 13, 2013, 03:34:07 PM »
I see no reason to change 3.0.5's code here, and don't know why you are adding +1. 3.0.5's  NGUITools.CalculateRaycastDepth already returns the widget's depth if it's present:
  1.         static public int CalculateRaycastDepth (GameObject go)
  2.         {
  3.                 UIWidget w = go.GetComponent<UIWidget>();
  4.                 if (w != null) return w.raycastDepth;