Advertisement
Guest User

Untitled

a guest
Jun 7th, 2019
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class GenerateUIButtons : MonoBehaviour
  7. {
  8. public Button buttonPrefab;
  9. public GameObject parent;
  10. public int numberOfButtons;
  11. public bool backColor = false;
  12.  
  13. private Button[] buttons;
  14. private List<Color> originalTextColors = new List<Color>();
  15. private List<Color> originalNormalColors = new List<Color>();
  16.  
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. buttons = new Button[numberOfButtons];
  21.  
  22. for (int i = 0; i < numberOfButtons; i++)
  23. {
  24. buttons[i] = Instantiate(buttonPrefab) as Button;
  25. var buttonText = buttons[i].GetComponentInChildren<Text>();
  26. if (i < Rotate.names.Length)
  27. {
  28. buttonText.text = Rotate.names[i];
  29. buttons[i].name = Rotate.names[i];
  30. }
  31. buttons[i].transform.SetParent(parent.transform, false);
  32. int j = i;
  33. originalTextColors.Add(buttonText.color);
  34. originalNormalColors.Add(buttons[i].GetComponent<Image>().color);
  35. buttons[i].onClick.AddListener(() => ButtonClicked(j));
  36. }
  37. }
  38.  
  39. void ButtonClicked(int buttonNo)
  40. {
  41. Debug.Log("Clicked On " + buttons[buttonNo]);
  42.  
  43. if (backColor == false)
  44. {
  45. var text = buttons[buttonNo].GetComponentInChildren<Text>();
  46. if (text.color == Color.red)
  47. {
  48. text.color = originalTextColors[buttonNo];
  49. }
  50. else
  51. {
  52. text.color = Color.red;
  53. }
  54. }
  55. else
  56. {
  57. var imageColor = buttons[buttonNo].GetComponent<Image>();
  58. if (imageColor.color == Color.green)
  59. {
  60. imageColor.color = originalNormalColors[buttonNo];
  61. }
  62. else
  63. {
  64. imageColor.color = Color.green;
  65. }
  66. }
  67. }
  68.  
  69. // Update is called once per frame
  70. void Update()
  71. {
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement