I create a grid with no initial children.
I add children to the grid list this (pardon the naming convention, I do a LOT of C++ code, so I try to keep things named consistently everywhere because otherwise it is too hard to manage all of the codebases (2+ million lines of code)
public void Refresh()
{
string[] arrstrCharacterNames = new string[] {"John", "Johnny" };
Transform oGridTransform = GlobalUtilities.FindChild("Grid", this.transform);
UIGrid oGrid = oGridTransform.GetComponent<UIGrid>();
//-- Remove any existing children...
RemoveGridItems();
//-- Iterate through the retrieved available character names for this User...
foreach(string strCharacterName in arrstrCharacterNames)
{
//-- Create a button and add it to the grid containing the list of characters...
GameObject oChild = NGUITools.AddChild(oGridTransform.gameObject, this.PlayerNameButton);
SelectCharacterNameButton oChildSettings = oChild.GetComponent<SelectCharacterNameButton>();
//-- Once the button has been added, set the character name accordingly...
oChildSettings.CharacterName = strCharacterName;
}
//-- We are done adding the character names, have the grid reposition the items as needed...
oGrid.Reposition();
}
That works; I close the "window" by deactivating it, when I do, I also remove the children from the grid like this:
private void RemoveGridItems()
{
Transform oGridTransform = GlobalUtilities.FindChild("Grid", this.transform);
UIGrid oGrid = oGridTransform.GetComponent<UIGrid>();
//-- Remove any existing children...
List<Transform> lstChildren = new List<Transform>();
foreach (Transform oChild in oGridTransform)
{
lstChildren.Add(oChild);
}
oGridTransform.DetachChildren();
foreach (Transform oTransform in lstChildren)
{
oTransform.gameObject.SetActive(false);
DestroyImmediate(oTransform.gameObject);
}
oGrid.Reposition();
}
Looking in the scene, I can see the items are gone.
I re-activate the "window", and add buttons (using the same code); except now, the buttons are overlapped. The two buttons appear as children and using the inspector everything looks ok.
Any idea how to correct this?