- Home /
Question by
kityanlam3 · Jun 24, 2016 at 06:37 AM ·
c#buttonslistsimages
Assign images to list of buttons
I have a list of images and a list of buttons that are to be dynamically generated. What I want to do is to assign each button an image from the list, if there are not enough images I want to then assign a default image to it.
Something like the code below, it's wrong but I think it gets what I want through. Thanks for any help.
for (int i = 0; i < list.Count; i++)
{
int index = i;
GameObject btn = (GameObject)Instantiate(Button);
//btn.transform.parent = canvas;
btn.transform.SetParent(canvas, false);
btn.GetComponentInChildren<Text>().text = Path.GetFileNameWithoutExtension(list[i]);
Button b = btn.GetComponent<Button>();
if (i == gfi.Count)
{
b.image.sprite = Sprite.Create(gfi[i], new Rect(0, 0, 100, 100), new Vector2());
}
else
{
b.image.sprite = Sprite.Create(_default, new Rect(0, 0, 100, 100), new Vector2());
}
b.onClick.RemoveAllListeners();
//Debug.Log(index);
b.onClick.AddListener(() => spawngroundfurniture(index));
btn.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
One List of GameObjects of different types... 0 Answers
How can you make images appear on (UI) buttons in a scroll box/scrollable list/scrollview? 0 Answers
Is it possible to choose from preinitialized lists in the inspector? 1 Answer