Author Topic: UIGrid  (Read 78419 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
UIGrid
« on: November 23, 2013, 03:06:56 AM »
Overview

UIGrid is a helper script that lets you easily arrange widgets into a fixed-size grid. It can be used both at edit time as well as run-time. If you want variable size cells instead, you can use the UITable instead.



To use UIGrid, select a panel, right-click anywhere in the scene view, and choose the Grid from the [/b]Create menu. You may also simply attach the UIGrid component to any empty game object.



The initial Arrangement determines which way the children will be positioned. Horizontal means they will extend toward the right, and Vertical means they will extend downwards.

Max Per Line field controls the number of columns in the Horizontal arrangement, and the number of rows in the Vertical arrangement.

Cell Width and Height determines the spacing between the items within the grid.

By default, the Grid will simply reposition all of its children, and the order will be the order that the children happen to have been created. If you want to change this and sort them in a specific order, you can name them alphabetically ("001", "002", "003", etc), and check the Sorted checkbox. Doing so will make the Grid sort them in order first before adjusting their position.

Lastly, if you want to keep the spacing left by invisible (disabled) children, turn off the Hide Inactive flag. By default this flag is on, and invisible children are simply ignored.

Pro-Tip

The Grid is useful for positioning things at run-time, but you can also execute it at Edit time. Simply right-click it and choose the Execute option. You can then safely delete the component if you don't need it.



Class Documentation

http://tasharen.com/ngui/docs/class_u_i_grid.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
« Last Edit: February 04, 2014, 03:18:38 AM by ArenMook »

optimisticmonkey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 15
    • View Profile
Re: UIGrid
« Reply #1 on: December 07, 2013, 12:42:10 PM »
How do I center the grid in the parent container?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid
« Reply #2 on: December 07, 2013, 04:11:28 PM »
Grid always begins at its origin point. To center it, just offset its local position by (number of children * cell height or width * 0.5).

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
Re: UIGrid
« Reply #3 on: December 08, 2013, 06:06:54 AM »
The problem is when you have a dynamic number of children that change at runtime.. You cannot set this from editor..

Would be great to be able to set the pivot point for the grid

charliehelman

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 16
    • View Profile
Re: UIGrid
« Reply #4 on: January 11, 2014, 03:53:54 PM »
The problem is when you have a dynamic number of children that change at runtime.. You cannot set this from editor..

Would be great to be able to set the pivot point for the grid

I have the same issue. It would be nice if this functionality was supported by UIGrid. Still, doesn't seem like a workaround would be too complicated. I'll see if I can't get something working myself.

Edit: I modified the UIGrid script to add this functionality. I added a drop down menu to the UIGrid component in inspector for selecting alignment (right, center, and left) and based on that selection modified the local position of the grid accordingly in the Reposition() method. Is it okay if I share it the code?

My additions:
  1.         public enum Alignment
  2.         {
  3.                 Left,
  4.                 Centered,
  5.                 Right,
  6.         }
  7.  
  8.         public Alignment alignment = Alignment.Left;
  9.  
  10. // ...Snip...
  11.  
  12.                 switch (alignment)
  13.                 {
  14.                         case Alignment.Left :
  15.                                 myTrans.localPosition = new Vector3 (0f, myTrans.localPosition.y, myTrans.localPosition.z);
  16.                                 break;
  17.  
  18.                         case Alignment.Centered :
  19.                                 float centerOffset = myTrans.childCount * cellWidth * 0.5f;
  20.                                 centerOffset = -centerOffset + (centerOffset * 1/myTrans.childCount);
  21.                                
  22.                                 myTrans.localPosition = new Vector3 (centerOffset, myTrans.localPosition.y, myTrans.localPosition.z);
  23.                                 break;
  24.  
  25.                         case Alignment.Right :
  26.                                 myTrans.localPosition = new Vector3 (-(myTrans.childCount - 1) * cellWidth, myTrans.localPosition.y, myTrans.localPosition.z);
  27.                                 break;
  28.                 }
  29.  

The entire file:
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. /// <summary>
  10. /// All children added to the game object with this script will be repositioned to be on a grid of specified dimensions.
  11. /// If you want the cells to automatically set their scale based on the dimensions of their content, take a look at UITable.
  12. /// </summary>
  13.  
  14. [AddComponentMenu("NGUI/Interaction/Grid")]
  15. public class UIGrid : UIWidgetContainer
  16. {
  17.         public delegate void OnReposition ();
  18.  
  19.         public enum Arrangement
  20.         {
  21.                 Horizontal,
  22.                 Vertical,
  23.         }
  24.  
  25.         public enum Alignment
  26.         {
  27.                 Left,
  28.                 Centered,
  29.                 Right,
  30.         }
  31.  
  32.         public Alignment alignment = Alignment.Left;
  33.  
  34.         /// <summary>
  35.         /// Type of arrangement -- vertical or horizontal.
  36.         /// </summary>
  37.  
  38.         public Arrangement arrangement = Arrangement.Horizontal;
  39.  
  40.         /// <summary>
  41.         /// Maximum children per line.
  42.         /// If the arrangement is horizontal, this denotes the number of columns.
  43.         /// If the arrangement is vertical, this stands for the number of rows.
  44.         /// </summary>
  45.  
  46.         public int maxPerLine = 0;
  47.  
  48.         /// <summary>
  49.         /// The width of each of the cells.
  50.         /// </summary>
  51.  
  52.         public float cellWidth = 200f;
  53.  
  54.         /// <summary>
  55.         /// The height of each of the cells.
  56.         /// </summary>
  57.  
  58.         public float cellHeight = 200f;
  59.  
  60.         /// <summary>
  61.         /// Whether the grid will smoothly animate its children into the correct place.
  62.         /// </summary>
  63.  
  64.         public bool animateSmoothly = false;
  65.  
  66.         /// <summary>
  67.         /// Whether the children will be sorted alphabetically prior to repositioning.
  68.         /// </summary>
  69.  
  70.         public bool sorted = false;
  71.  
  72.         /// <summary>
  73.         /// Whether to ignore the disabled children or to treat them as being present.
  74.         /// </summary>
  75.  
  76.         public bool hideInactive = true;
  77.  
  78.         /// <summary>
  79.         /// Whether the parent container will be notified of the grid's changes.
  80.         /// </summary>
  81.  
  82.         public bool keepWithinPanel = false;
  83.  
  84.         /// <summary>
  85.         /// Callback triggered when the grid repositions its contents.
  86.         /// </summary>
  87.  
  88.         public OnReposition onReposition;
  89.  
  90.         /// <summary>
  91.         /// Reposition the children on the next Update().
  92.         /// </summary>
  93.  
  94.         public bool repositionNow { set { if (value) { mReposition = true; enabled = true; } } }
  95.  
  96.         bool mReposition = false;
  97.         UIPanel mPanel;
  98.         bool mInitDone = false;
  99.  
  100.         void Init ()
  101.         {
  102.                 mInitDone = true;
  103.                 mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  104.         }
  105.  
  106.         void Start ()
  107.         {
  108.                 if (!mInitDone) Init();
  109.                 bool smooth = animateSmoothly;
  110.                 animateSmoothly = false;
  111.                 Reposition();
  112.                 animateSmoothly = smooth;
  113.                 enabled = false;
  114.         }
  115.  
  116.         void Update ()
  117.         {
  118.                 if (mReposition) Reposition();
  119.                 enabled = false;
  120.         }
  121.  
  122.         static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  123.  
  124.         /// <summary>
  125.         /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary.
  126.         /// </summary>
  127.  
  128.         [ContextMenu("Execute")]
  129.         public void Reposition ()
  130.         {
  131.                 if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
  132.                 {
  133.                         mReposition = true;
  134.                         return;
  135.                 }
  136.  
  137.                 if (!mInitDone) Init();
  138.  
  139.                 mReposition = false;
  140.                 Transform myTrans = transform;
  141.  
  142.                 int x = 0;
  143.                 int y = 0;
  144.  
  145.                 if (sorted)
  146.                 {
  147.                         List<Transform> list = new List<Transform>();
  148.  
  149.                         for (int i = 0; i < myTrans.childCount; ++i)
  150.                         {
  151.                                 Transform t = myTrans.GetChild(i);
  152.                                 if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);
  153.                         }
  154.                         list.Sort(SortByName);
  155.  
  156.                         for (int i = 0, imax = list.Count; i < imax; ++i)
  157.                         {
  158.                                 Transform t = list[i];
  159.  
  160.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  161.  
  162.                                 float depth = t.localPosition.z;
  163.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  164.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  165.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  166.  
  167.                                 if (animateSmoothly && Application.isPlaying)
  168.                                 {
  169.                                         SpringPosition.Begin(t.gameObject, pos, 15f);
  170.                                 }
  171.                                 else t.localPosition = pos;
  172.  
  173.                                 if (++x >= maxPerLine && maxPerLine > 0)
  174.                                 {
  175.                                         x = 0;
  176.                                         ++y;
  177.                                 }
  178.                         }
  179.                 }
  180.                 else
  181.                 {
  182.                         for (int i = 0; i < myTrans.childCount; ++i)
  183.                         {
  184.                                 Transform t = myTrans.GetChild(i);
  185.  
  186.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  187.  
  188.                                 float depth = t.localPosition.z;
  189.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  190.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  191.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  192.  
  193.                                 if (animateSmoothly && Application.isPlaying)
  194.                                 {
  195.                                         SpringPosition.Begin(t.gameObject, pos, 15f);
  196.                                 }
  197.                                 else t.localPosition = pos;
  198.  
  199.                                 if (++x >= maxPerLine && maxPerLine > 0)
  200.                                 {
  201.                                         x = 0;
  202.                                         ++y;
  203.                                 }
  204.                         }
  205.                 }
  206.  
  207.                 switch (alignment)
  208.                 {
  209.                         case Alignment.Left :
  210.                                 myTrans.localPosition = new Vector3 (0f, myTrans.localPosition.y, myTrans.localPosition.z);
  211.                                 break;
  212.  
  213.                         case Alignment.Centered :
  214.                                 float centerOffset = myTrans.childCount * cellWidth * 0.5f;
  215.                                 centerOffset = -centerOffset + (centerOffset * 1/myTrans.childCount);
  216.                                
  217.                                 myTrans.localPosition = new Vector3 (centerOffset, myTrans.localPosition.y, myTrans.localPosition.z);
  218.                                 break;
  219.  
  220.                         case Alignment.Right :
  221.                                 myTrans.localPosition = new Vector3 (-(myTrans.childCount - 1) * cellWidth, myTrans.localPosition.y, myTrans.localPosition.z);
  222.                                 break;
  223.                 }
  224.  
  225.                 if (keepWithinPanel && mPanel != null)
  226.                         mPanel.ConstrainTargetToBounds(myTrans, true);
  227.  
  228.                 if (onReposition != null)
  229.                         onReposition();
  230.         }
  231. }
  232.  
  233.  
I'm very new to programming so I apologize if I made a mistake or my additions are hard to read.
« Last Edit: January 11, 2014, 06:39:53 PM by charliehelman »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid
« Reply #5 on: January 11, 2014, 06:35:27 PM »
Sure, thanks.

mmASH

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UIGrid
« Reply #6 on: January 13, 2014, 07:20:43 PM »
Should we wait for any updates in new version about this, or will be better write some custom script?  ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid
« Reply #7 on: January 14, 2014, 06:49:54 PM »
UIGrid and UITable improvements are on the list of things to do.

mmASH

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UIGrid
« Reply #8 on: January 15, 2014, 10:11:56 AM »
ok, thanks will be waiting for that
also nice to have in UIGgrid something like auto detect for columns count, so it will fit itself into parent  :)

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
Re: UIGrid
« Reply #9 on: January 18, 2014, 02:11:06 PM »
This is a modified script we use.. I took the original script from this forum somewhere and it's slightly modified

The problems that appear are with the children pivot point but if you use the same for all, you should be cool

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2013 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. /// <summary>
  10. /// All children added to the game object with this script will be repositioned to be on a grid of specified dimensions.
  11. /// If you want the cells to automatically set their scale based on the dimensions of their content, take a look at UITable.
  12. /// </summary>
  13. using System;
  14.  
  15. [AddComponentMenu("NGUI/Interaction/GridPivot")]
  16. public class UIGridPivot : UIWidgetContainer
  17. {
  18.     public enum Arrangement
  19.     {
  20.         Horizontal,
  21.         Vertical,
  22.     }
  23.    
  24.     public enum Pivot
  25.     {
  26.         TopLeft,
  27.         TopCenter,
  28.         TopRight,
  29.         MiddleLeft,
  30.         MiddleCenter,
  31.         MiddleRight,
  32.         BottomLeft,
  33.         BottomCenter,
  34.         BottomRight
  35.     }
  36.    
  37.     /// <summary>
  38.     /// Where should the Pivot point of the grid be?
  39.     /// </summary>
  40.    
  41.     public Pivot pivot;
  42.    
  43.     /// <summary>
  44.     /// Type of arrangement -- vertical or horizontal.
  45.     /// </summary>
  46.    
  47.     public Arrangement arrangement = Arrangement.Horizontal;
  48.    
  49.     /// <summary>
  50.     /// Maximum children per line.
  51.     /// If the arrangement is horizontal, this denotes the number of columns.
  52.     /// If the arrangement is vertical, this stands for the number of rows.
  53.     /// </summary>
  54.    
  55.     public int maxPerLine = 0;
  56.    
  57.     /// <summary>
  58.     /// The width of each of the cells.
  59.     /// </summary>
  60.    
  61.     public float cellWidth = 200f;
  62.    
  63.     /// <summary>
  64.     /// The height of each of the cells.
  65.     /// </summary>
  66.    
  67.     public float cellHeight = 200f;
  68.    
  69.     /// <summary>
  70.     /// Whether the grid will smoothly animate its children into the correct place.
  71.     /// </summary>
  72.    
  73.     public bool animateSmoothly = false;
  74.    
  75.     /// <summary>
  76.     /// Whether the children will be sorted alphabetically prior to repositioning.
  77.     /// </summary>
  78.    
  79.     public bool sorted = false;
  80.    
  81.     /// <summary>
  82.     /// Whether to ignore the disabled children or to treat them as being present.
  83.     /// </summary>
  84.    
  85.     public bool hideInactive = true;
  86.    
  87.     /// <summary>
  88.     /// Reposition the children on the next Update().
  89.     /// </summary>
  90.    
  91.     public bool repositionNow { set { if (value) { mReposition = true; enabled = true; } } }
  92.    
  93.     bool mStarted = false;
  94.     bool mReposition = false;
  95.    
  96.     void Start ()
  97.     {
  98.         mStarted = true;
  99.         bool smooth = animateSmoothly;
  100.         animateSmoothly = false;
  101.         Reposition();
  102.         animateSmoothly = smooth;
  103.         enabled = false;
  104.     }
  105.    
  106.     void Update ()
  107.     {
  108.         if (mReposition) Reposition();
  109.         enabled = false;
  110.     }
  111.    
  112.     static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  113.    
  114.     /// <summary>
  115.     /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary.
  116.     /// </summary>
  117.    
  118.     [ContextMenu("Execute")]
  119.     public void Reposition ()
  120.     {
  121.         if (Application.isPlaying && !mStarted)
  122.         {
  123.             mReposition = true;
  124.             return;
  125.         }
  126.        
  127.         mReposition = false;
  128.         Transform myTrans = transform;
  129.  
  130.         // Current column
  131.         int column = 0;
  132.  
  133.         // Current row
  134.         int row = 0;
  135.        
  136.         List<Transform> list = new List<Transform>();
  137.        
  138.         for (int i = 0; i < myTrans.childCount; ++i)
  139.         {
  140.             Transform t = myTrans.GetChild(i);
  141.             if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);
  142.         }
  143.        
  144.         if (sorted)
  145.         {
  146.             list.Sort(SortByName);
  147.         }
  148.  
  149.         int columns = list.Count - 1;
  150.         if (maxPerLine > 0)
  151.         {
  152.             columns = list.Count > maxPerLine ? maxPerLine : list.Count - 1;
  153.         }
  154.        
  155.         int rows = 1;
  156.         if (maxPerLine > 0)
  157.         {
  158.             rows = (int)Math.Ceiling((double)list.Count / maxPerLine);
  159.         }
  160.        
  161.         for (int i = 0, imax = list.Count; i < imax; ++i)
  162.         {
  163.             Transform currentChild = list[i];
  164.            
  165.             if (!NGUITools.GetActive(currentChild.gameObject) && hideInactive) continue;
  166.            
  167.             float depth = currentChild.localPosition.z;
  168.             Vector3 pos = new Vector3(0, 0, depth);
  169.            
  170.             switch (pivot)
  171.             {
  172.                 case Pivot.TopLeft:
  173.                 case Pivot.TopCenter:
  174.                 case Pivot.TopRight:
  175.                     pos.y = -cellHeight * row;
  176.                     break;
  177.                 case Pivot.MiddleLeft:
  178.                 case Pivot.MiddleCenter:
  179.                 case Pivot.MiddleRight:
  180.                     pos.y = -cellHeight * (-rows / 2f + row + 0.5f);
  181.                     break;
  182.                 case Pivot.BottomLeft:
  183.                 case Pivot.BottomCenter:
  184.                 case Pivot.BottomRight:
  185.                     pos.y = -cellHeight * (row + 1 - rows);// -cellHeight * lines + cellHeight * y;
  186.                     break;
  187.                 default:
  188.                     break;
  189.             }
  190.            
  191.             switch (pivot)
  192.             {
  193.                 case Pivot.TopLeft:
  194.                 case Pivot.MiddleLeft:
  195.                 case Pivot.BottomLeft:
  196.                     pos.x = cellWidth * column;
  197.                     break;
  198.                 case Pivot.TopCenter:
  199.                 case Pivot.MiddleCenter:
  200.                 case Pivot.BottomCenter:
  201.                     pos.x = cellWidth * (-columns / 2f + column); // -cellWidth * (columns / 2f) + cellWidth * x;
  202.                     break;
  203.                 case Pivot.TopRight:
  204.                 case Pivot.MiddleRight:
  205.                 case Pivot.BottomRight:
  206.                     pos.x = cellWidth * (column - 1 - columns);
  207.                     break;
  208.                 default:
  209.                     break;
  210.             }
  211.            
  212.             if (animateSmoothly && Application.isPlaying)
  213.             {
  214.                 SpringPosition.Begin(currentChild.gameObject, pos, 15f);
  215.             }
  216.             else currentChild.localPosition = pos;
  217.            
  218.             if (++column >= maxPerLine && maxPerLine > 0)
  219.             {
  220.                 column = 0;
  221.                 ++row;
  222.             }
  223.         }
  224.        
  225.         //Debug.LogError(transform.localPosition + " total width : " + totalWidth);
  226.        
  227.         UIScrollView drag = NGUITools.FindInParents<UIScrollView>(gameObject);
  228.         if (drag != null) drag.UpdateScrollbars(true);
  229.     }
  230. }

seanb

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UIGrid
« Reply #10 on: February 01, 2014, 08:16:01 PM »
What is the correct way to reposition a vertical grid after loading dynamic data? I used to have this working correctly before UIDraggablePanel was converted to UIScrollView, but I can't seem to get it working since the change. I have searched the forum thoroughly and tried all advice you and others have given. My (condensed) old way of doing it:

  1.  
  2. public GameObject myGrid;
  3. public GameObject myCellPrefab;
  4.  
  5. IEnumerator loadGrid(ArrayList data){
  6.        
  7.         //Empty Grid
  8.         foreach(Transform child in myGrid.transform){
  9.                 NGUITools.Destroy(child.gameObject);
  10.         }
  11.        
  12.         //Load New Data
  13.         foreach(string item_name in data){
  14.                 GameObject item = NGUITools.AddChild(myGrid, myCellPrefab);
  15.                 item.FindChild("Label").GetComponent<UILabel>().text = item_name;
  16.         }
  17.  
  18.         //Adding WaitForEndOfFrame() used to work, now does not help
  19.         //yield return new WaitForEndOfFrame();
  20.  
  21.         myGrid.GetComponent<UIGrid>().Reposition();
  22.  
  23. }
  24.  

I've tried the following with no luck:
- Adding "yield return new WaitForEndOfFrame();" after each block
- Adding "repositionNow=true" after emptying the grid, after loading the grid, or both
- Adding "Reposition()" after emptying the grid, after loading the grid, or both
- Adding "ResetPosition()" to the UIScrollView.
- Using DestroyImmediate instead of Destroy

It looks find on the first load, but after the second load everything is misaligned because the cells are not being destroyed before Reposition() is called. Each load doubles the entries because the items are not being destroyed at all.

-edit-
After struggling with this for days, I found that the only way to get this working correctly is to use Unity's Destroy() to destroy all old entries, followed by WaitForEndOfFrame() . NGUITools.Destroy() does not work in my case, no matter what I try it retains old entries that were supposed to be destroyed.
« Last Edit: February 03, 2014, 02:39:03 PM by seanb »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid
« Reply #11 on: February 04, 2014, 03:23:49 AM »
You should not be using NGUITools.Destroy while iterating through the list of children, as NGUITools.Destroy unparents the object before destroying it. Unity's own Destroy function does not. So you can Destroy an object, then iterate through the list of children and it would still be there as if nothing happened.

Instead of doing that, do this:
  1. while (myGrid.transform.childCount > 0)
  2.     NGUITools.Destroy(myGrid.transform.GetChild(0).gameObject);
  3. // ...add new data, then:
  4. myGrid.GetComponent<UIGrid>().Reposition();
No need to wait for the next frame either.

MOST2K2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIGrid
« Reply #12 on: February 04, 2014, 12:42:20 PM »
Cant find a simple example how to add components programmatically to UIGrid.
I want to use it as a highscore table.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid
« Reply #13 on: February 05, 2014, 04:43:03 PM »
Use the same NGUITools.AddChild() to add children to a game object that has the UIGrid script attached. UIGrid works with the game object's children. It doesn't have any other lists.

MOST2K2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UIGrid
« Reply #14 on: February 06, 2014, 12:21:45 PM »
Ok ArenMook.
Could you give any advice how to make a simple scrollable highscore board with 2 columns (player, score)?