Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of contacts. I need to create a scrollable list of buttons that are created dynamically so I can add each contact name as the button text. How would I do this all in code? My biggest concern is making sure the sizes stay consistent. I haven't had to do that in code before.

EDIT:

enter image description here

enter image description here

And here is the code to add the buttons to this:

for (int i = 0; i < count; i++)
{
    if (i > 0)
        crt.sizeDelta = new Vector2(crt.sizeDelta.x, crt.sizeDelta.y + initHeight);
    GameObject contactButton = Instantiate(Resources.Load("Contact Button")) as GameObject;
    contactButton.transform.parent = contactsList.transform;
    contactButton.GetComponentInChildren<Text>().text = names[i];
}

enter image description here

share|improve this question

enter image description here

ScrollRectHolder holds ScrollRect component. The content of it is ScrollContent. Add Vertical Layout Group component on ScrollContent. It's also the parent of the buttons that will be instantiated each time you need. Button is the button prefab that will be instantiated. The variable initialHeight is the initial height of ScrollContent. Have to get it from Rect Transform of ScrollContent. Need to update sizeDelta with adding initialHeight whenever you add a new button.

public void AddNewButton()
{
 mRect.sizeDelta = new Vector2(mRect.sizeDelta.x, mRect.sizeDelta.y + initialHeight);
 GameObject gO = Instantiate(buttonPrefab);
 gO.transform.parent = transform;
}

enter image description here

enter image description here

enter image description here

unitypackage with scene, script and prefab. Unity 5.2.4f1.

share|improve this answer
    
I gave this a shot but for some reason the buttons show up at the top left of the ScrollContent and are very small. I have the width and height constraint of the Vertical Layout Group turned off. Turning either on just makes the buttons way bigger than the screen. Any ideas? – saboehnke Jul 18 at 23:35
    
check how ScrollRectHolder, ScrollContent are placed and anchored in edit. Last one is output. – SP. Jul 18 at 23:55
    
I don't have a button already set in the ScrollContent since I am trying to create them all dynamically as I get the names. Will that be an issue? I have the ScrollContent size set to the same size as the prefab button is but the buttons still show up as tiny dots when they are created. They are automatically set to width: 10 and height: 10 for some reason. It says "some values driven by VerticalLayoutGroup" – saboehnke Jul 19 at 0:36
    
If you want to keep the ScrollContent empty then ignore sizeDelta update first time or change initial Height dynamically. Most of the variable fields of Rect Transform are open to change runtime. Once your setup is right, there shouldn't be any problem. Share some shots of your current setup. – SP. Jul 19 at 4:50
1  

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.