Author Topic: Anchoring prefab items in UITable  (Read 1948 times)

Dalediko

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Anchoring prefab items in UITable
« on: May 29, 2014, 04:10:17 AM »
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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Anchoring prefab items in UITable
« Reply #1 on: May 29, 2014, 07:52:56 AM »
Prefabs can't keep references to anything outside of their own hierarchy because prefabs don't exist as a part of the hierarchy.

You also forgot to call ResetAnchors() and UpdateAnchors() after changing the anchors. Look inside the UIRect.SetAnchor function.

Dalediko

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Anchoring prefab items in UITable
« Reply #2 on: May 29, 2014, 10:24:44 AM »
Perfect!!

Of course, like you said, I didn't try to keep external references in the prefab -
I had to explicitly set them into the object after instantiation...
But indeed I forgot to call ResetAnchors() and UpdateAnchors().

If anyone else ever encounters this problem - here's my fixed code.
It works like a charm! TNX A lot  ;)


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);

item.ResetAnchors();
item.UpdateAnchors();

table.GetComponent<UITable>().Reposition();