2

I am trying to make a custom list in ScrollView, but I can't manage to do it in new UI.
I have a prefab that is a Panel with 5 Text elements side by side. Now, there is ScrollView on scene with one horizontal and one vertical Scrollbar and I want to Instantiate 5 of those prefabs one below another so they look like a table.
Here is what I did, but it only instantiates them on top of each other and stretches them:

    for(int i=0; i<5; i++)
    {
        GameObject q = Instantiate(questionObjectModel) as GameObject;
        q.transform.parent = questionListHolder.GetComponent<Transform>();
    }

questionObjectModel is the ScrollView on the scene (I did try to set another Panel inside and_Istantiate_ prefabs on it).

Edit:
Ok, I have managed to set them not to be stretched. Only need to set them one below another now:

q.transform.SetParent(questionListHolder.transform, false);
filipst
  • 1,547
  • 1
  • 30
  • 55

1 Answers1

5

I did it with help of a friend.
Inside of a ScrollView add new Panel and on add components Vertical Layout Group and Content Size Filter on that panel. When you do that, on prefab that you are instantiating set component Layout Element and set preferred size if you want to.
Then just instantiate prefab with:

        GameObject q = Instantiate(questionObjectModel) as GameObject;
        q.transform.SetParent(questionListHolder.transform, false);

You can see instantiating explanation on http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

I hope it helps.

filipst
  • 1,547
  • 1
  • 30
  • 55