123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 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<Text>();
- btnImage = GetComponent<Image>();
-
- 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);
- }
- }
- /// <summary>
- /// 注销状态
- /// </summary>
- public void Cancellation()
- {
- CancellationState?.Invoke();
- }
- /// <summary>
- /// 显示/隐藏子物体
- /// </summary>
- /// <param name="show"></param>
- public void ShowOrHideChild(bool show = true)
- {
- foreach (var item in childButtonItems)
- {
- item.gameObject.SetActive(show);
- }
- }
- }
|