Author Topic: Going insane working with clip panels and grids...  (Read 3215 times)

storm33229

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 44
    • View Profile
Going insane working with clip panels and grids...
« on: September 29, 2013, 02:15:17 PM »
I have one clip panel and underneath it a grid. I dynamically add buttons to it, and remove buttons from it (when they are clicked). The desired behavior is that the grid reorganizes itself after each add/remove so that there are not giant gaps between buttons.

So I have:

  1.    
  2.     private IEnumerator CoUpdateDragPanelAndGrid()
  3.     {
  4.         //update the grid
  5.         m_unitListGrid.Reposition();
  6.  
  7.         //put a frame between updating the drag panel and the grid
  8.         yield return null;
  9.  
  10.         //update the drag panel
  11.         if (m_dragPanel != null)
  12.         {
  13.             m_dragPanel.RestrictWithinBounds(false);
  14.             m_dragPanel.ResetPosition();
  15.         }
  16.     }

This is called after add/remove of a button:

Add:
  1.         GameObject unitButtonObj = Instantiate(unitButtonPrefab) as GameObject;
  2.  
  3.         unitButtonObj.transform.parent = m_unitListGrid.transform;
  4.  
  5.         unitButtonObj.transform.localScale = Vector3.one;
  6.  
  7.         EditTeamUnitButton button = null;
  8.  
  9.         unitButtonObj.VerifyComponentRecursively(ref button);
  10.  
  11.         //setup the button to act as a drag content for the draggable panel
  12.         UIDragPanelContents dragContents = null;
  13.  
  14.         if (unitButtonObj.VerifyComponentRecursively(ref dragContents))
  15.         {
  16.             dragContents.draggablePanel = m_dragPanel;
  17.         }
  18.  

Remove:

  1.             Destroy(button.gameObject);
  2.  
  3.             StartCoroutine(CoUpdateDragPanelAndGrid());
  4.  

I've attached some images that show what I am seeing... Interestingly, if I reverse the update code to be:

  1.     private IEnumerator CoUpdateDragPanelAndGrid()
  2.     {
  3.         //update the drag panel
  4.         if (m_dragPanel != null)
  5.         {
  6.             m_dragPanel.RestrictWithinBounds(false);
  7.             m_dragPanel.ResetPosition();
  8.         }
  9.  
  10.         //put a frame between updating the drag panel and the grid
  11.         yield return null;
  12.  
  13.         //update the grid
  14.         m_unitListGrid.Reposition();
  15.     }
  16.  

If I do this, the removal works, but on  trying to Add back it jumps the clip panel center to one side...and cuts off my buttons. The workaround I am using right now is using the first method for adding, and the second method for removing.. I really want to know what I am doing wrong in the first place though..

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Going insane working with clip panels and grids...
« Reply #1 on: September 29, 2013, 07:05:16 PM »
Ok first of all, use NGUITools.AddChild instead of Instantiate. It parents the object, sets its layer, and resets its pos/rot/scale for you all in one go.

Second, use yield return new WaitUntilEndOfFrame(); or whatever that command is called so that you don't skip a frame.

Lastly... use NGUITools.Destroy instead of just Destroy. It unparents the object for you. If you don't do this, Unity doesn't actually remove the object from the hierarchy until the end of frame -- which is why reposition call doesn't work for you.

storm33229

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 44
    • View Profile
Re: Going insane working with clip panels and grids...
« Reply #2 on: September 29, 2013, 07:20:18 PM »
Ok first of all, use NGUITools.AddChild instead of Instantiate. It parents the object, sets its layer, and resets its pos/rot/scale for you all in one go.

Second, use yield return new WaitUntilEndOfFrame(); or whatever that command is called so that you don't skip a frame.

Lastly... use NGUITools.Destroy instead of just Destroy. It unparents the object for you. If you don't do this, Unity doesn't actually remove the object from the hierarchy until the end of frame -- which is why reposition call doesn't work for you.

Wow this information is really helpful! I had been wondering if I was missing something as far as adding/removing widgets was concerned...apparently I was  ;D

Thanks for your help, this totally fixed the issue.