ActionButtonCreator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4. /// <summary>
  5. /// Simple script to help create action button prefabs programmatically
  6. /// This can be attached to a prefab or used to create buttons at runtime
  7. /// </summary>
  8. public class ActionButtonCreator : MonoBehaviour
  9. {
  10. [Header("Button Creation")]
  11. public bool createButtonOnStart = false;
  12. public string buttonText = "Action";
  13. public Sprite buttonIcon;
  14. void Start()
  15. {
  16. if (createButtonOnStart)
  17. {
  18. CreateActionButton();
  19. }
  20. }
  21. /// <summary>
  22. /// Creates a complete action button with icon and text
  23. /// </summary>
  24. public static GameObject CreateActionButton(Transform parent, string buttonName = "ActionButton")
  25. {
  26. // Create main button object
  27. GameObject buttonObj = new GameObject(buttonName);
  28. buttonObj.transform.SetParent(parent, false);
  29. // Add RectTransform
  30. RectTransform rectTransform = buttonObj.AddComponent<RectTransform>();
  31. rectTransform.sizeDelta = new Vector2(60, 60);
  32. // Add Image component for background
  33. Image backgroundImage = buttonObj.AddComponent<Image>();
  34. backgroundImage.color = new Color(0.2f, 0.2f, 0.2f, 0.8f);
  35. backgroundImage.type = Image.Type.Sliced;
  36. // Add Button component
  37. Button button = buttonObj.AddComponent<Button>();
  38. // Create Icon child
  39. GameObject iconObj = new GameObject("Icon");
  40. iconObj.transform.SetParent(buttonObj.transform, false);
  41. RectTransform iconRect = iconObj.AddComponent<RectTransform>();
  42. iconRect.anchorMin = new Vector2(0.1f, 0.3f);
  43. iconRect.anchorMax = new Vector2(0.9f, 0.9f);
  44. iconRect.offsetMin = Vector2.zero;
  45. iconRect.offsetMax = Vector2.zero;
  46. Image iconImage = iconObj.AddComponent<Image>();
  47. iconImage.color = Color.white;
  48. // Create Label child
  49. GameObject labelObj = new GameObject("Label");
  50. labelObj.transform.SetParent(buttonObj.transform, false);
  51. RectTransform labelRect = labelObj.AddComponent<RectTransform>();
  52. labelRect.anchorMin = new Vector2(0f, 0f);
  53. labelRect.anchorMax = new Vector2(1f, 0.3f);
  54. labelRect.offsetMin = Vector2.zero;
  55. labelRect.offsetMax = Vector2.zero;
  56. TextMeshProUGUI labelText = labelObj.AddComponent<TextMeshProUGUI>();
  57. labelText.text = "Action";
  58. labelText.fontSize = 8;
  59. labelText.alignment = TextAlignmentOptions.Center;
  60. labelText.color = Color.white;
  61. // Setup button colors
  62. ColorBlock colors = button.colors;
  63. colors.normalColor = new Color(0.2f, 0.2f, 0.2f, 0.8f);
  64. colors.highlightedColor = new Color(0.3f, 0.3f, 0.3f, 0.9f);
  65. colors.pressedColor = new Color(0.1f, 0.1f, 0.1f, 0.9f);
  66. colors.selectedColor = new Color(0.3f, 0.3f, 0.3f, 0.9f);
  67. colors.disabledColor = new Color(0.1f, 0.1f, 0.1f, 0.5f);
  68. button.colors = colors;
  69. return buttonObj;
  70. }
  71. private void CreateActionButton()
  72. {
  73. GameObject button = CreateActionButton(transform, buttonText);
  74. // Set icon if provided
  75. if (buttonIcon != null)
  76. {
  77. Image iconImage = button.transform.Find("Icon").GetComponent<Image>();
  78. if (iconImage != null)
  79. iconImage.sprite = buttonIcon;
  80. }
  81. // Set text
  82. TextMeshProUGUI labelText = button.transform.Find("Label").GetComponent<TextMeshProUGUI>();
  83. if (labelText != null)
  84. labelText.text = buttonText;
  85. }
  86. }