Thursday, December 15, 2016

unity - Creating a list of buttons dynamically in code


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 = names[i];
}

enter image description here





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...