Author Topic: Remove and destroy children from a grid? (Candidate for NGUITools?)  (Read 9293 times)

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Surely there must be an easier way than this to delete all of the items in a UIGrid?

      Transform oGridTransform = this.FindChild("Grid", this.transform);
      UIGrid oGrid = oGridTransform.GetComponent<UIGrid>();

      //-- First, iterate through the grid, and remove any children that are present...
      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);
      }

What am I missing?

Why isn't there an NGUITools.RemoveChild()?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
« Reply #1 on: January 05, 2014, 09:00:22 AM »
  1. while (transform.childCount > 0)
  2.     NGUITools.Destroy(transform.GetChild(0).gameObject);

chad

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
« Reply #2 on: January 07, 2014, 02:01:30 PM »
I am having a similar problem, I am creating a bunch of buttons at runtime in a grid, but the buttons are not being destroyed afterwards.

Here is how I add the buttons:

  1. GameObject grid = GameObject.Find("Logic Grid");
  2.  
  3. int length = PlayEditorStrings.szStandard.Length - 1;
  4.  
  5. for (int i = 0; i < length; ++i)
  6. {
  7.     GameObject player = NGUITools.AddChild(grid, Resources.Load("Logic Button") as GameObject);
  8.    
  9.     player.name = PlayEditorStrings.szStandard[i];
  10.     player.GetComponentInChildren<UILabel>().text = PlayEditorStrings.szStandard[i];
  11. }
  12.  

And here is what I am calling on OnApplicationQuit()

  1. GameObject grid = GameObject.Find("Logic Grid");
  2.  
  3. if (grid != null)
  4. {
  5.     Transform trans = grid.transform;
  6.  
  7.     while (trans.childCount > 0)
  8.     {
  9.         NGUITools.Destroy(trans.GetChild(0).gameObject);
  10.     }
  11. }

However, none of the objects are ever being destroyed, and will pile up if I run it multiple times.

I was doing something similar with UILabel prefabs, and it works fine, but using a simple button made as a prefab doesn't work.

chad

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
« Reply #3 on: January 07, 2014, 05:52:54 PM »
Nevermind. It was actually working, however, my initialize function was being called again after the OnApplicationQuit call. I still don't know why, but I have fixed that problem.

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
« Reply #4 on: April 14, 2014, 09:16:14 AM »
Nevermind. It was actually working, however, my initialize function was being called again after the OnApplicationQuit call. I still don't know why, but I have fixed that problem.

Can I ask how did you fixed that issue? (my initialize function was being called again after the OnApplicationQuit call)

Thanks.