Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: jeffpk on March 11, 2014, 04:11:41 PM

Title: Grid reposition problems on dynamic menu
Post by: jeffpk on March 11, 2014, 04:11:41 PM
Hey,

I'm having a really odd problem.  This is my code to create a menu with dynamically assigned buttons:

  1. public void AddItem(string txt, Action act)
  2.         {
  3.                 GameObject buttonObject = NGUITools.AddChild(grid,buttonPrototype);
  4.                 UIButton button = buttonObject.GetComponent<UIButton>();
  5.                 button.onClick.Add(new EventDelegate(new EventDelegate.Callback(act)));
  6.                 UILabel label = buttonObject.transform.Find("Label").GetComponent<UILabel>();
  7.                 label.text = txt;
  8.                 NGUITools.AdjustDepth(buttonObject,11);
  9.         }
  10.  
  11.         public void Layout(){
  12.                 grid.GetComponent<UIGrid>().repositionNow = true;
  13.                 grid.GetComponent<UIGrid>().Reposition();
  14.  
  15.         }
  16.  

I call AddItem to add each button and then Layout when they are a lll added.

The layout doesn't happen and they end up stacked up on each other on screen HOWEVER if I go to the UIGrid in the explorer and trigger Execute it lays out fine.

The bizarre thing is that, from what I can see, all Execute does is call Reposition().

So, I'm at a loss as to why my code doesn't work.

Help?
Title: Re: Grid reposition problems on dynamic menu
Post by: jeffpk on March 11, 2014, 04:24:37 PM
I'm also seeing this exception when the panel becomes visible. Maybe its part of the problem?

Unlit/Transparent doesn't have a clipped shader version for SoftClip
UnityEngine.Debug:LogError(Object)
UIDrawCall:CreateMaterial() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:310)
UIDrawCall:RebuildMaterial() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:325)
UIDrawCall:UpdateMaterials() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:347)
UIDrawCall:OnWillRenderObject() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:547)
Title: Re: Grid reposition problems on dynamic menu
Post by: jeffpk on March 11, 2014, 04:40:51 PM
Fixed the exception, still doesn't layout :(
Title: Re: Grid reposition problems on dynamic menu
Post by: ArenMook on March 11, 2014, 05:40:59 PM
Reposition() won't happen if the game object or the grid script is inactive. Likewise it will not happen until after Start() gets called. Calling Reposition() before Start() will simply delay the Reposition execution until after the script starts up. Did you try adding Debug.Logs in there to see how far it gets?
Title: Re: Grid reposition problems on dynamic menu
Post by: jeffpk on March 11, 2014, 07:43:42 PM
AH, its hidden when i update it.  Thats likely the reason, thank you, checking now..
Title: Re: Grid reposition problems on dynamic menu
Post by: jeffpk on March 11, 2014, 07:45:49 PM
That solved it!  Thank you so much, I never would have guessed that on my own.
Title: Re: Grid reposition problems on dynamic menu
Post by: zeroZshadow on May 13, 2014, 06:52:30 AM
Could you please share how you fixed this?
I'm having some issues with this myself.

I've made a popup window, that should show a scrollview filled with bars.
To "spawn" the popup window, i call Enable on the script that controlls it, which sets the gameObject.setActive(true)
After this i spawn the bars using the NGUITools and do the grid.Reposition();
And while they do reposition, they have wierd spacing between them.

This happens every time i close and reopen the window (active false and true again)
However if i do grid.repositionNow = true; Instead. The wierd spacing only happens the FIRST time it opens.
And looks like it should the next time.

I'm very interested in how you worked around this.
Title: Re: Grid reposition problems on dynamic menu
Post by: ArenMook on May 13, 2014, 09:06:20 AM
The "weird spacing" can occur if you try to reposition the content before the grid's settings get set, whether via Unity's serialization or otherwise. Put a Debug.Log in the grid's reposition function printing out the cell size. Might give you an idea.
Title: Re: Grid reposition problems on dynamic menu
Post by: zeroZshadow on May 14, 2014, 06:25:45 PM
It seemed that i need tried adding things to a grid/scrollview BEFORE its first start.
Making the function adding the items a coroutine and putting a yield before the reposition command, fixed my issue

Now i do have a quick question, why does the grid init on start, not on OnEnabled?
Title: Re: Grid reposition problems on dynamic menu
Post by: ArenMook on May 15, 2014, 12:10:12 AM
Because of the call order. Consider this:
  1. UIGrid grid = AddComponent<UIGrid>();
  2. grid.cellWidth = 20f;
  3. grid.cellHeight = 20f;
  4. grid.Reposition();
What will actually happen is:
  1. UIGrid grid = AddComponent<UIGrid>();
  2. // Awake is called
  3. // OnEnable is called
  4. grid.cellWidth = 20f;
  5. grid.cellHeight = 20f;
  6. grid.Reposition();
  7. // Start is called some time later