ButtonItem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. public class ButtonItem : MonoBehaviour {
  8. [Header("正常状态图片")]
  9. public Sprite normalSprite;
  10. [Header("选中图片")]
  11. public Sprite selectedSprite;
  12. [Header("选中字体颜色")]
  13. public Color fontSelectColor;
  14. [Header("正常字体颜色")]
  15. public Color fontNomalColor = Color.white;
  16. [Header("子物体字体大小")]
  17. public int childFontSize;
  18. public ButtonItem[] childButtonItems;
  19. public UnityEvent BtnClickState;
  20. public UnityEvent CancellationState;
  21. private Image btnImage;
  22. private Text fontText;
  23. private GameObject rightBtn;
  24. private GameObject downBtn;
  25. private void Awake()
  26. {
  27. fontText = transform.Find("Text").GetComponent<Text>();
  28. btnImage = GetComponent<Image>();
  29. if (transform.Find("RightBtn")) rightBtn = transform.Find("RightBtn").gameObject;
  30. if (transform.Find("DownBtn")) downBtn = transform.Find("DownBtn").gameObject;
  31. }
  32. private void Start()
  33. {
  34. Init();
  35. }
  36. public void Init()
  37. {
  38. if (childButtonItems == null || childButtonItems.Length == 0)
  39. {
  40. if (rightBtn != null) rightBtn.gameObject.SetActive(false);
  41. if (downBtn != null) downBtn.gameObject.SetActive(false);
  42. }
  43. else
  44. {
  45. for (int i = 0; i < childButtonItems.Length; i++)
  46. {
  47. childButtonItems[i].fontText.fontSize = childFontSize;
  48. childButtonItems[i].gameObject.SetActive(false);
  49. }
  50. }
  51. }
  52. public void Select()
  53. {
  54. btnImage.sprite = selectedSprite;
  55. fontText.color = fontSelectColor;
  56. }
  57. public void UnSelct()
  58. {
  59. btnImage.sprite = normalSprite;
  60. fontText.color = fontNomalColor;
  61. }
  62. public void Click()
  63. {
  64. BtnClickState?.Invoke();
  65. fontText.color = fontSelectColor;
  66. if (childButtonItems.Length == 0) return;
  67. if (rightBtn.gameObject.activeSelf)
  68. {
  69. downBtn.gameObject.SetActive(true);
  70. rightBtn.gameObject.SetActive(false);
  71. ShowOrHideChild(true);
  72. }
  73. else
  74. {
  75. rightBtn.gameObject.SetActive(true);
  76. downBtn.gameObject.SetActive(false);
  77. ShowOrHideChild(false);
  78. }
  79. }
  80. /// <summary>
  81. /// 注销状态
  82. /// </summary>
  83. public void Cancellation()
  84. {
  85. CancellationState?.Invoke();
  86. }
  87. /// <summary>
  88. /// 显示/隐藏子物体
  89. /// </summary>
  90. /// <param name="show"></param>
  91. public void ShowOrHideChild(bool show = true)
  92. {
  93. foreach (var item in childButtonItems)
  94. {
  95. item.gameObject.SetActive(show);
  96. }
  97. }
  98. }