Author Topic: Grid is not well relocated when delete all children and create again  (Read 4257 times)

xiangting

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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Grid is not well relocated when delete all children and create again
« Reply #1 on: April 20, 2015, 08:14:27 PM »
Post some code. How do you add / remove entries?

xiangting

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Grid is not well relocated when delete all children and create again
« Reply #2 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();
   }
   

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Grid is not well relocated when delete all children and create again
« Reply #3 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