Author Topic: Quick Q about ordering  (Read 7602 times)

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Quick Q about ordering
« on: March 17, 2014, 10:42:13 PM »
Hey,

Im adding some elements to my NGUI gui at run time and even though they are lower precedence then the prebuilt gui items they are showing ontop of them.

Is there a call I need to make to kick NGUi into reprioritizing everything properly?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Quick Q about ordering
« Reply #1 on: March 18, 2014, 03:10:43 PM »
When the panels are left at default settings (Automatic Render Q), then everything is based on depth.

First everything is sorted by panel depth, and only then by widget depth. This means that panel depth is more important than widget depth. If depths are the same, it won't be clear what should be in front of what.

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #2 on: March 18, 2014, 05:53:12 PM »
Do you mean that all the children of one panel should be above or below all the children of another panel?

Because thats not what I'm seeing.  Take a look at the images below.  The images inside of the "tile Picker" are added at run-time.  They show up above the pop up script editor which is its own complex hierarchy, but the rest of the tile picker is hidden.

The root sprite of the script editor starts at depth 20 and all its children are higher.

The tile images are all at depth 8.

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #3 on: March 18, 2014, 06:14:16 PM »
Took a look at it in Panel View

The TilePicker has one Panel which is a parent of the grid which contains the tile images that are bleeding through.

It has a depth of 1.  The images have a depth of 3.

All of the Script editor depths are 20 or greater, including all the panels.

This really seems to be a result of those images having been added to the tile picker in code.  If you are curious, here is the code that adds them:

  1.  
  2.         UIGrid grid = GetComponent<UIGrid>();
  3.                 UIWidget widget = transform.parent.GetComponent<UIWidget>();
  4.                 foreach(TileRecord rec in tileRecords){
  5.                         GameObject panel = new GameObject();
  6.                         UITexture nguiTexture = panel.AddComponent<UITexture>();
  7.                         nguiTexture.mainTexture = rec.tilePreviewImage;
  8.                         nguiTexture.depth = widget.depth+1;
  9.                         nguiTexture.transform.localScale = new Vector3(0.5f,0.5f,1);
  10.                         panel.transform.parent = transform;
  11.                         panel.transform.localPosition = Vector3.zero;
  12.                         nguiTexture.MarkAsChanged();
  13.                         NGUITools.AddWidgetCollider(panel);
  14.                         UIDragScrollView dsv = panel.AddComponent<UIDragScrollView>();
  15.                         dsv.scrollView = grid.transform.parent.GetComponent<UIScrollView>();
  16.                         TileClickManager tcm = panel.AddComponent<TileClickManager>();
  17.                         tcm.tileRec = rec;
  18.                         tcm.mapCursor = cursor;
  19.                 }
  20.         grid.Reposition();
  21.                 transform.parent.GetComponent<UIWidget>().MarkAsChanged();
  22.  
« Last Edit: March 18, 2014, 07:27:59 PM by jeffpk »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Quick Q about ordering
« Reply #4 on: March 18, 2014, 09:17:56 PM »
First... never use Instantiate or "new GameObject".

Use NGUITools.AddChild. If you don't, then the layers won't be set properly, the parent may not be set properly, and you are very likely to start using the widget before its parent is set, which in effect will cause it to be re-parented by the system, which is not what you want. When you then go to change the parent afterwards, the widget won't know that you did so unless you call MarkParentAsChanged().

NGUITools.AddChild saves a lot of problems. So does NGUITools.AddWidget for that matter.

Oh and also... you are creating a new game object called "panel", then add a UITexture to it, which is a widget. There is no panel there. Worse yet, widgets must be children of a panel, or shit hits the fan. Note how when you create a widget in Unity via ALT+SHIFT+W or drag & drop a "Control" into the scene view in a brand-new scene the UI hierarchy gets created for you along with UIRoot?

This is important.

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #5 on: March 18, 2014, 09:54:19 PM »
THanks.  It would probably save you some time if you eventually created a manual that laid out the organizational paradigms for NGUI and how things go together.

I'll  try applying all this and see what happens.

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #6 on: March 19, 2014, 10:40:03 AM »
Hmm. Code now looks like this.  No visual change in result

  1. UIGrid grid = GetComponent<UIGrid>();
  2.                 UIWidget widget = transform.parent.GetComponent<UIWidget>();
  3.                 foreach(TileRecord rec in tileRecords){
  4.                         GameObject panel = NGUITools.AddChild(grid.gameObject);
  5.                         //UITexture nguiTexture = panel.AddComponent<UITexture>();
  6.                         UITexture nguiTexture = NGUITools.AddWidget<UITexture>(panel);
  7.                         nguiTexture.mainTexture = rec.tilePreviewImage;
  8.                         nguiTexture.depth = widget.depth+1;
  9.                         //nguiTexture.transform.localScale = new Vector3(0.5f,0.5f,1);
  10.                         panel.transform.parent = transform;
  11.                         panel.transform.localPosition = Vector3.zero;
  12.                         nguiTexture.MarkAsChanged();
  13.                         NGUITools.AddWidgetCollider(panel);
  14.                         //UIDragScrollView dsv = panel.AddComponent<UIDragScrollView>();
  15.                         UIDragScrollView dsv = NGUITools.AddMissingComponent<UIDragScrollView>(panel);
  16.                         dsv.scrollView = grid.transform.parent.GetComponent<UIScrollView>();
  17.                         //TileClickManager tcm = panel.AddComponent<TileClickManager>();
  18.                         TileClickManager tcm = NGUITools.AddMissingComponent<TileClickManager>(panel);
  19.                         tcm.tileRec = rec;
  20.                         tcm.mapCursor = cursor;
  21.                 }
  22.                 grid.Reposition();
  23.                 transform.parent.GetComponent<UIWidget>().MarkAsChanged();
  24.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Quick Q about ordering
« Reply #7 on: March 20, 2014, 01:46:12 AM »
Why are you doing this?
  1.             panel.transform.parent = transform;
  2.             panel.transform.localPosition = Vector3.zero;

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #8 on: March 20, 2014, 10:55:12 AM »
Ah, the second is to reset position.  Mostly I wanted to make sure the Z was set to 0.  But I'
ll take it out.

The first you are right is just dinosaur code i missed removing and might be the problem!

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #9 on: March 20, 2014, 11:21:27 AM »
Unfortunately its still doing the exact same thing.

I stripped down that code to remove all dinosaur code.  It currently looks like this:

  1. void Start () {
  2.                 MapCursor cursor = FindObjectOfType<MapCursor>();
  3.                 List<TileRecord> tileRecords = GameManager.currentTileMap.Tileset.tileList;
  4.                 UIGrid grid = GetComponent<UIGrid>();
  5.                 UIWidget widget = transform.parent.GetComponent<UIWidget>();
  6.                 foreach(TileRecord rec in tileRecords){
  7.                         GameObject panel = NGUITools.AddChild(grid.gameObject);
  8.                         UITexture nguiTexture = NGUITools.AddWidget<UITexture>(panel);
  9.                         nguiTexture.mainTexture = rec.tilePreviewImage;
  10.                         nguiTexture.depth = widget.depth+1;
  11.                         NGUITools.AddWidgetCollider(panel);
  12.                         UIDragScrollView dsv = NGUITools.AddMissingComponent<UIDragScrollView>(panel);
  13.                         dsv.scrollView = grid.transform.parent.GetComponent<UIScrollView>();
  14.                         TileClickManager tcm = NGUITools.AddMissingComponent<TileClickManager>(panel);
  15.                         tcm.tileRec = rec;
  16.                         tcm.mapCursor = cursor;
  17.                 }
  18.                 grid.Reposition();
  19.                 widget.MarkAsChanged();
  20.         }
  21.  

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #10 on: March 20, 2014, 11:38:55 AM »
Found it!

For some reason,. creating a child between the grid and the Texture widget was causing the problem.  (I didn't realize that AddWidget actually creates an entire game object, like Instantiate does when give a component.)

Thanks for all your help!

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #11 on: March 21, 2014, 06:00:06 PM »
Spoke too son.

The aberrant ordering behavior has returned just like before.

This is the current code:
  1. void Start () {
  2.                 MapCursor cursor = FindObjectOfType<MapCursor>();
  3.                 List<TileRecord> tileRecords = GameManager.currentTileMap.Tileset.tileList;
  4.                 UIGrid grid = GetComponent<UIGrid>();
  5.                 UIWidget widget = transform.parent.GetComponent<UIWidget>();
  6.                 foreach(TileRecord rec in tileRecords){
  7.                         UITexture nguiTexture = NGUITools.AddWidget<UITexture>(gameObject);
  8.                         GameObject panel = nguiTexture.gameObject;
  9.                         nguiTexture.mainTexture = rec.tilePreviewImage;
  10.                         nguiTexture.depth = widget.depth+1;
  11.                         NGUITools.AddWidgetCollider(panel);
  12.                         UIDragScrollView dsv = NGUITools.AddMissingComponent<UIDragScrollView>(panel);
  13.                         dsv.scrollView = grid.transform.parent.GetComponent<UIScrollView>();
  14.                         TileClickManager tcm = NGUITools.AddMissingComponent<TileClickManager>(panel);
  15.                         tcm.tileRec = rec;
  16.                         tcm.mapCursor = cursor;
  17.                 }
  18.                 grid.Reposition();
  19.                 widget.MarkAsChanged();
  20.         }
  21.  

Are there any component settings I should check?

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Quick Q about ordering
« Reply #12 on: March 21, 2014, 06:05:43 PM »
NM Im figuring it out.

It has to do with the fact that some widgets have their own UIPanel and some don't.

UIPanel seems pretty key, its purpose and use should really be explained better and right up front as the first thing you talk about in documentation