Author Topic: UIGrid Layout issue (3.0.8f4) - Add+DestroyItems+add = overlapped items  (Read 2312 times)

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
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?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid Layout issue (3.0.8f4) - Add+DestroyItems+add = overlapped items
« Reply #1 on: January 05, 2014, 08:53:57 AM »
Add some Debug.Log statements to UIGrid.Reposition to see where it exits out early.

The following test seems to work as expected:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public GameObject prefab;
  6.  
  7.         void OnEnable ()
  8.         {
  9.                 NGUITools.AddChild(gameObject, prefab);
  10.                 NGUITools.AddChild(gameObject, prefab);
  11.                 NGUITools.AddChild(gameObject, prefab);
  12.                 GetComponent<UIGrid>().Reposition();
  13.         }
  14.  
  15.         void OnDisable ()
  16.         {
  17.                 for (int i = 0; i < transform.childCount; ++i)
  18.                         Destroy(transform.GetChild(i).gameObject);
  19.         }
  20. }