Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class GenerateUIButtons : MonoBehaviour
- {
- public Button buttonPrefab;
- public GameObject parent;
- public int numberOfButtons;
- public bool backColor = false;
- private Button[] buttons;
- private List<Color> originalTextColors = new List<Color>();
- private List<Color> originalNormalColors = new List<Color>();
- // Start is called before the first frame update
- void Start()
- {
- buttons = new Button[numberOfButtons];
- for (int i = 0; i < numberOfButtons; i++)
- {
- buttons[i] = Instantiate(buttonPrefab) as Button;
- var buttonText = buttons[i].GetComponentInChildren<Text>();
- if (i < Rotate.names.Length)
- {
- buttonText.text = Rotate.names[i];
- buttons[i].name = Rotate.names[i];
- }
- buttons[i].transform.SetParent(parent.transform, false);
- int j = i;
- originalTextColors.Add(buttonText.color);
- originalNormalColors.Add(buttons[i].GetComponent<Image>().color);
- buttons[i].onClick.AddListener(() => ButtonClicked(j));
- }
- }
- void ButtonClicked(int buttonNo)
- {
- Debug.Log("Clicked On " + buttons[buttonNo]);
- if (backColor == false)
- {
- var text = buttons[buttonNo].GetComponentInChildren<Text>();
- if (text.color == Color.red)
- {
- text.color = originalTextColors[buttonNo];
- }
- else
- {
- text.color = Color.red;
- }
- }
- else
- {
- var imageColor = buttons[buttonNo].GetComponent<Image>();
- if (imageColor.color == Color.green)
- {
- imageColor.color = originalNormalColors[buttonNo];
- }
- else
- {
- imageColor.color = Color.green;
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement