Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Prodigga on June 26, 2014, 09:00:27 PM

Title: What is the 'NGUI way' of adding widgets/etc programmatically
Post by: Prodigga on June 26, 2014, 09:00:27 PM
I am having a lot of issues with objects not updating correctly when I add things programmatically (UIStretch doesn't stretch to fill contents, etc (many more cases similar to this)). Is there some 'proper' way to add ui elements to the scene programmatically that forces all the relevant objects to refresh correctly? I keep having size and position errors in my elements. The only way I can avoid this issue if the wait atleast one frame and then call reset position (or something similar, ie reposition on UIGrid). This only works after I wait a frame. So my code is quickly becoming very messy especially when I need to refresh an element inside an element - I need to wait a frame, refresh the parent, wait a frame and refresh the child.

Is there some NGUI function that will ensure things are correctly updated?
Title: Re: What is the 'NGUI way' of adding widgets/etc programmatically
Post by: ArenMook on June 27, 2014, 12:54:17 PM
UIStretch is a legacy script that stopped being relevant back in NGUI 3.0.7. You need to use the anchoring system. Check the documentation for UIRect.

http://www.tasharen.com/forum/index.php?topic=7013.0

Immediate refresh of the UI is done via NGUITools.ImmediatelyCreateDrawCalls.
Title: Re: What is the 'NGUI way' of adding widgets/etc programmatically
Post by: Prodigga on June 30, 2014, 02:47:36 AM
Ok well.. UIStretch was a bad example. But the problem is very real. Adding things to existing widgets programatically is a massive pain. Most things just don't work. I think I am not doing something correctly. Everything always works so perfectly in the editor but when i replicate those steps at run time through code, i get funny results.

I've attached a repo project to this post. Import the latest NGUI (3.6.6 as of writing this) to your project and import my package. Check out the 2 scenes. In one of them, I've precreated a scrollable list of items that stretch to fit the scroll view (resize the viewport and you'll see this in action). In the second scene I am doing it through code and it just doesn't work even though the outcome appears to be the exact same in the inspector.

What am I doing wrong, or what else should I be doing?
Title: Re: What is the 'NGUI way' of adding widgets/etc programmatically
Post by: ArenMook on July 01, 2014, 03:13:09 AM
Simple mistakes, but 4 of them.

1. You need to use NGUITools.AddChild, not Instantiate. It sets the parent object right away, as well as the game object's layer and a few other things. Look inside that function to find out more.

2. You were setting the right anchor's relative value to 0 instead of 1. 0 is 'left'. 1 is 'right'. So both left and right were effectively the same thing.

3. You should be modifying the anchors, not doing a "new UIRect.AnchorPoint".

4. You never called ResetAnchors() and UpdateAnchors(). Look at UIRect.SetAnchor. Note how it calls those at the end?

Here is your PopulateList() function, fixed and optimized.
  1.         void PopulateList()
  2.         {
  3.                 for(int i = 0; i<NumToAdd;i++)
  4.                 {
  5.                         GameObject newListItem = NGUITools.AddChild(TheGrid.gameObject, ThePrefab);
  6.                         UIWidget newListItemsWidget = newListItem.GetComponent<UIWidget>();
  7.  
  8.                         newListItemsWidget.leftAnchor.Set(TheScrollView.transform, 0f, 0f);
  9.                         newListItemsWidget.rightAnchor.Set(TheScrollView.transform, 1f, 0f);
  10.                         newListItemsWidget.ResetAnchors();
  11.                         newListItemsWidget.UpdateAnchors();
  12.                 }
  13.                
  14.                 TheGrid.Reposition();
  15.                 TheScrollView.ResetPosition();
  16.         }
Title: Re: What is the 'NGUI way' of adding widgets/etc programmatically
Post by: Prodigga on July 02, 2014, 12:51:21 AM
Awesome stuff, thanks. The AddChild + ResetAnchors + UpdateAnchors functions solved a lot of issues for me. Cheers!