Hey there!
I'm developing a mobile app, where the screen is actually a vertically scrollable list of items.
The items are basically sprites, which always fill the width of the screen, but their height
depends on their contents (child sprites and labels).
So:
My scene's hierarchy looks like this: UIRoot -> ScrollView -> UITable (vertical) -> items.
ScrollView is anchored to UIRoot (left-anchor to left-of-parent, right-anchor to right-of-parent, etc.)
And each of the table items is anchored as follows ("Advanced" anchoring):
Left anchor: attached to the scroll view's left
Right anchor: attached to the scroll view's right
Top anchor: Unset (I leave it empty so the table can take care of positioning the items vertically)
Bottom anchor: attached to the bottom of the lowest child or this item (so the item's height will
automatically change according to the item's contents).
This works BEAUTIFULLY!!
But only when my items are static, instantiated in advance.
When I try to instantiate the items using prefabs, it doesn't work anymore :-(
It's totally strange - in the scene view - everything looks fine, all the parameters are correct.
But in the game view - I can't see the items.
Only when I manually change one of the item's parameters in the scene view
(e.g., change "Right anchor" numeric value from 0 to 1), then change it back to 0...
only then the item magically appears in the table.
I tried to do the same in code (change a parameter, then change it back) -
but couldn't reach the same effect :-(
Prefabs are created as follows:
public GameObject table;
public GameObject scrollView;
public GameObject itemPrefab;
GameObject item = Instantiate(itemPrefab, table.transform.position, table.transform.rotation) as GameObject;
item.transform.parent = table.transform;
item.transform.localScale = Vector3.one;
item.leftAnchor.target = scrollView.transform;
item.rightAnchor.target = scrollView.transform;
item.leftAnchor.Set(0, 0);
item.rightAnchor.Set(1f, 0);
table.GetComponent<UITable>().Reposition();
What am I doing wrong?
Any ideas?
TNX