using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class ButtonItem : MonoBehaviour { [Header("正常状态图片")] public Sprite normalSprite; [Header("选中图片")] public Sprite selectedSprite; [Header("选中字体颜色")] public Color fontSelectColor; [Header("正常字体颜色")] public Color fontNomalColor = Color.white; [Header("子物体字体大小")] public int childFontSize; public ButtonItem[] childButtonItems; public UnityEvent BtnClickState; public UnityEvent CancellationState; private Image btnImage; private Text fontText; private GameObject rightBtn; private GameObject downBtn; private void Awake() { fontText = transform.Find("Text").GetComponent(); btnImage = GetComponent(); if (transform.Find("RightBtn")) rightBtn = transform.Find("RightBtn").gameObject; if (transform.Find("DownBtn")) downBtn = transform.Find("DownBtn").gameObject; } private void Start() { Init(); } public void Init() { if (childButtonItems == null || childButtonItems.Length == 0) { if (rightBtn != null) rightBtn.gameObject.SetActive(false); if (downBtn != null) downBtn.gameObject.SetActive(false); } else { for (int i = 0; i < childButtonItems.Length; i++) { childButtonItems[i].fontText.fontSize = childFontSize; childButtonItems[i].gameObject.SetActive(false); } } } public void Select() { btnImage.sprite = selectedSprite; fontText.color = fontSelectColor; } public void UnSelct() { btnImage.sprite = normalSprite; fontText.color = fontNomalColor; } public void Click() { BtnClickState?.Invoke(); fontText.color = fontSelectColor; if (childButtonItems.Length == 0) return; if (rightBtn.gameObject.activeSelf) { downBtn.gameObject.SetActive(true); rightBtn.gameObject.SetActive(false); ShowOrHideChild(true); } else { rightBtn.gameObject.SetActive(true); downBtn.gameObject.SetActive(false); ShowOrHideChild(false); } } /// /// 注销状态 /// public void Cancellation() { CancellationState?.Invoke(); } /// /// 显示/隐藏子物体 /// /// public void ShowOrHideChild(bool show = true) { foreach (var item in childButtonItems) { item.gameObject.SetActive(show); } } }