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:
private IEnumerator CoUpdateDragPanelAndGrid()
{
//update the grid
m_unitListGrid.Reposition();
//put a frame between updating the drag panel and the grid
yield return null;
//update the drag panel
if (m_dragPanel != null)
{
m_dragPanel.RestrictWithinBounds(false);
m_dragPanel.ResetPosition();
}
}
This is called after add/remove of a button:
Add:
GameObject unitButtonObj = Instantiate(unitButtonPrefab) as GameObject;
unitButtonObj.transform.parent = m_unitListGrid.transform;
unitButtonObj.transform.localScale = Vector3.one;
EditTeamUnitButton button = null;
unitButtonObj.VerifyComponentRecursively(ref button);
//setup the button to act as a drag content for the draggable panel
UIDragPanelContents dragContents = null;
if (unitButtonObj.VerifyComponentRecursively(ref dragContents))
{
dragContents.draggablePanel = m_dragPanel;
}
Remove:
Destroy(button.gameObject);
StartCoroutine(CoUpdateDragPanelAndGrid());
I've attached some images that show what I am seeing... Interestingly, if I reverse the update code to be:
private IEnumerator CoUpdateDragPanelAndGrid()
{
//update the drag panel
if (m_dragPanel != null)
{
m_dragPanel.RestrictWithinBounds(false);
m_dragPanel.ResetPosition();
}
//put a frame between updating the drag panel and the grid
yield return null;
//update the grid
m_unitListGrid.Reposition();
}
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..