Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: John.Bergman on January 05, 2014, 07:07:07 AM

Title: Remove and destroy children from a grid? (Candidate for NGUITools?)
Post by: John.Bergman on January 05, 2014, 07:07:07 AM
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()?
Title: Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
Post by: ArenMook on January 05, 2014, 09:00:22 AM
  1. while (transform.childCount > 0)
  2.     NGUITools.Destroy(transform.GetChild(0).gameObject);
Title: Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
Post by: chad 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.
Title: Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
Post by: chad 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.
Title: Re: Remove and destroy children from a grid? (Candidate for NGUITools?)
Post by: hexaust 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.