0

I am using Unity 5.5.2f and NGUI 3.9.4 and I want to take advantage of NGUI's Grid to auto arrange items in perfect display.

The thing is that when I add items in the hierarchy manually things will work fine but when I add items through the script , Grid won't arrange my items.

Here is the code that I use to add items to level board. enter image description here

void Start () {

        List<Level> levels = ApplicationModel.Levels;
        GameObject currentWrap = null;
        Debug.LogWarning(levels.Count);
        for (int i = 0; i < levels.Count; i++)
        {
            if(i%4 == 0){

                currentWrap = Instantiate(levelWrap,transform,false);

            }
            if (levels[i].IsLock){

                GameObject go = Instantiate(levelLocked, currentWrap.transform, false);
                go.transform.localPosition = new Vector3(levelXstart + (i % 4) * levelXOffset, 0, 0);

                go.transform.GetComponentInChildren<UILabel>().text = i.ToString();
            }
            else
            {

                GameObject go = Instantiate(levelActive, currentWrap.transform, false);
                go.transform.localPosition = new Vector3(levelXstart + (i % 4) * levelXOffset, 0, 0);
                go.transform.GetComponentInChildren<UILabel>().text = i.ToString();
                for (int j = 0; j < levels[i].StarGet;j++){
                    go.transform.Find("fills").GetChild(j).gameObject.SetActive(true);
                }

            }
        }

    }
armnotstrong
  • 8,605
  • 16
  • 65
  • 130

1 Answers1

0

Ok, I find the trick, after Instantiate the wrap item, I should AddChild() to the grid system.

 if(i%4 == 0)
 {                  
   currentWrap = Instantiate(levelWrap,transform,false);
   GetComponent<UIGrid>().AddChild(currentWrap.transform);   
 }
armnotstrong
  • 8,605
  • 16
  • 65
  • 130