I have a widget which contains a table, and a label anchored to the bottom of the table. Like this:
+-------WIDGET----+
|+-------TABLE----+|
||row1 ||
|+-----------------+|
||row2 ||
|+-----------------+|
| SOME LABEL |
+-------------------+
I want to size the widget to the content. That is as the number of rows in the table determmines the overall height of the widget.
I tried doing it after I added the items to the table. Something like:
GameObject cell = NGUITools.AddChild(_table.gameObject, _darkPrefab);
cell.name = "1";
cell = NGUITools.AddChild(_table.gameObject, _lightPrefab);
cell.name = "2";
_table.Reposition();
Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(_label.gameObject.transform);
UIWidget widget = GetComponent<UIWidget>();
widget.height = (int)(bounds.center.y + bounds.extents.y);
The problem here is the label isn't reposition by the anchor until the next update.
I can put the resize code in the update, but I'm not sure that is really correct. Firstly, isn't there an issue with update order? Is there a guarantee that the anchor updates prior to the code which updates the widget? Secondly, I need to actually pad the size a bit since the panel is supposed to be a little taller than the content (think a border top and bottom for a window). If I do something like:
void Update()
{
Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(gameObject.transform);
Debug.Log(bounds);
UIWidget widget = GetComponent<UIWidget>();
widget.height = (int)bounds.extents.y * 2 + 20;
}
Then of course the panel grows and grows. I could use a bool to do it once of course, but then perhaps the update order can be incorrect?
Is there a simple way to do this?