Author Topic: Widget and panel depth  (Read 3278 times)

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Widget and panel depth
« on: October 09, 2014, 10:08:25 PM »
I recently created a little class that will essentially be a contextual touch area via a widget making use of UIEventListners OnClick() and initially it worked fine, but then i came across the problem that, I added it to a panel and that panel also contains a panel which is a message system so it should always be on top, or at the very least higher in the draw order. The issue then is that it eats up all the touch events before it can get to my touch window widget. So i thought...no problem ill just have the class create a panel on the fly...turns out this can't be done and I'm sure there is a good reason for that. So i settled on a compromise that the widget should look for a UIDragDropRoot Panel to parent to as more often than not that will be higher in the depth order, but even then i had reservations that i may sometimes want messages to overlay on top of my UIDragDropRoot panel, which at the moment the only course of action would be to change the depths of the panels at runtime.

So my question really in a round about way is, although this seems to work does anyone have any suggestions on how to do it better? code example follows

  1. public class WCTouchWindowListener : MonoBehaviour
  2. {
  3.         public delegate void onTouched();
  4.         private onTouched touched;
  5.         UIWidget widget;
  6.         Rect _dimensions;
  7.         bool _destroyOnTouch;
  8.        
  9.         public static void Create(onTouched touched, bool destroyTouch = true ,Rect dimensions = new Rect())
  10.         {
  11.                 if(!Application.isPlaying)return;
  12.  
  13.                 GameObject go = new GameObject("Touch window");
  14.  
  15.  
  16.                 go.AddComponent<WCTouchWindowListener>().Setup(touched,destroyTouch,dimensions);
  17.                 go.transform.parent = WCStateManager.Instance.State().transform;
  18.         }
  19.        
  20.         public void Setup(onTouched touched, bool destroyTouch = true, Rect dimensions = new Rect())
  21.         {
  22.                 _destroyOnTouch = destroyTouch;
  23.                 _dimensions = dimensions;
  24.  
  25.                 if(_dimensions == default(Rect))
  26.                         _dimensions = new Rect(0,0,UIRoot.list[0].GetComponent<UIPanel>().width,UIRoot.list[0].GetComponent<UIPanel>().height);
  27.  
  28.                 widget = gameObject.AddComponent<UIWidget>();
  29.                 NGUITools.AddWidgetCollider(gameObject);
  30.                 widget.autoResizeBoxCollider = true;
  31.  
  32.                 this.touched = touched;
  33.         }
  34.  
  35.         void Start()
  36.         {
  37.                 if(UIDragDropRoot.root != null)
  38.                         transform.parent = UIDragDropRoot.root;
  39.  
  40.                 widget.SetDimensions((int)_dimensions.width,(int)_dimensions.height);
  41.                 widget.SetRect(_dimensions.x-(_dimensions.width/2),_dimensions.y-(_dimensions.height/2),_dimensions.width,_dimensions.height);
  42.  
  43.                 transform.localScale = Vector3.one;
  44.                
  45.                 NGUITools.BringForward(gameObject);
  46.         }
  47.  
  48.         void OnClick ()
  49.         {
  50.                 if(touched != null)
  51.                 {
  52.                         touched();
  53.                 }
  54.  
  55.                 if(_destroyOnTouch)
  56.                 {
  57.                         if(Application.isPlaying)
  58.                                 GameObject.Destroy(gameObject);
  59.                         else if(Application.isEditor)
  60.                                 GameObject.DestroyImmediate(gameObject);
  61.                 }
  62.         }
  63. }
  64.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Widget and panel depth
« Reply #1 on: October 10, 2014, 02:48:46 AM »
Can't be done? Why? Just create a panel and give it a higher depth. Although in most cases you should already have this panel there, just in active. In Windward I have roughly 10 different panels, all disabled. I enable them as needed using the windowing system from Starlink (UIWindow.Show, UIWindow.Hide, etc).

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Widget and panel depth
« Reply #2 on: October 10, 2014, 03:15:11 AM »
I meant create it programatically during runtime, and i tried to do game.AddComponent<UIPanel>() and it did not produced the desired result...i cant remember what it did exactly...Then i looked through your code to see if there was any special functions to help me i came across NGUITools.CreateUI(); which just found the root ui and returned that so i then just instantly assumed you had a reason it would not let me create one properly runtime.

And your Windward example only makes this more apparent...so you just keep a bunch of unused panels and activate and reparent them as you wish and need?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Widget and panel depth
« Reply #3 on: October 11, 2014, 03:57:51 AM »
I don't reparent them at all. I adjust their depths if needed though. Why reparent things?

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Widget and panel depth
« Reply #4 on: October 15, 2014, 05:30:55 AM »
I suppose you don't need to reparent panels just make sure their depth is at the appropriate level.