Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: xiangting on April 20, 2015, 09:45:23 AM

Title: Grid is not well relocated when delete all children and create again
Post by: xiangting on April 20, 2015, 09:45:23 AM
My grid is in the scroll view. My action is to delete some labels in grid and regenerate some to it. Sometimes it appears in wrong position, and sometime it doesn't show up until I click on it. I've tried the UIWidget.MarkAsChanged(). But it does not work.
Title: Re: Grid is not well relocated when delete all children and create again
Post by: ArenMook on April 20, 2015, 08:14:27 PM
Post some code. How do you add / remove entries?
Title: Re: Grid is not well relocated when delete all children and create again
Post by: xiangting on April 21, 2015, 09:24:38 AM
I first delete all the children:
public void DeleteAllStory()
   {
      int count = grid.transform.childCount;

      for(int i = 0; i < count; i++ )
      {
         Destroy(grid.transform.GetChild(i).gameObject);
      }
   }
then generate new ones:
public void GetAllTheStory()
   {   
      foreach(Story s in bsl.Stories)
      {
         GameObject newButton = (GameObject)Instantiate(myStoryButton);
         newButton.name = s.StoryName;
         newButton.transform.parent = this.transform;
         newButton.transform.localScale = Vector3.one;      
         newButton.GetComponent<UILabel>().text = s.StoryName;
      }
      GetComponent<UIWidget>().MarkAsChanged();
   }
   
Title: Re: Grid is not well relocated when delete all children and create again
Post by: ArenMook on April 22, 2015, 02:00:21 PM
You need to use NGUITools.Destroy instead of just Destroy.

Unity's Destroy() call is delayed. It's not immediate. If you iterate through the list after destroying the children, you will see all of them still there.

Likewise, don't use Instantiate. Use NGUITools.AddChild. Read the sticky post at the top of this forum.

http://www.tasharen.com/forum/index.php?topic=11208.0