鸟语天空
UGUI—UISwitchButton
post by:追风剑情 2020-9-9 14:14
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
/// <summary>
/// 开关按钮
/// </summary>
public class UISwitchButton : MonoBehaviour, IPointerClickHandler
{
    public bool interactable = true;
    [SerializeField]
    private Text m_Text;
    [SerializeField]
    private Image m_Image;

    public Sprite openSprite;
    public Sprite closeSprite;

    public string openText;
    public string closeText;

    [SerializeField]
    private ButtonState m_State;

    #region 校验事件
    [Serializable]
    public class ValidateEventArgs
    {
        public bool cancel;
    }

    private ValidateEventArgs m_ValidateEventArgs = new ValidateEventArgs();
    [Serializable]
    public class ValidateEvent : UnityEvent<ValidateEventArgs> { }
    [FormerlySerializedAs("onValidate")]
    [SerializeField]
    private ValidateEvent m_OnValidate = new ValidateEvent();
    public ValidateEvent OnValidate
    {
        get { return m_OnValidate; }
    }
    #endregion

    #region 状态变化事件
    [Serializable]
    public class StateChangedEvent : UnityEvent<UISwitchButton> { }

    [FormerlySerializedAs("onChangedState")]
    [SerializeField]
    private StateChangedEvent m_OnChangedState = new StateChangedEvent();
    public StateChangedEvent OnChangedState
    {
        get { return m_OnChangedState; }
    }

    public enum ButtonState
    {
        Open,
        Close
    }
    #endregion

    public void Reset()
    {
        m_State = ButtonState.Open;
        UpdateDisplay();
    }

    private void Awake()
    {
        UpdateDisplay();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (interactable)
            OnClick();
    }

    public void UpdateDisplay()
    {
        if (m_State == ButtonState.Open)
        {
            if (m_Text != null)
                m_Text.text = openText;
            if (m_Image != null)
                m_Image.sprite = openSprite;
        }
        else
        {
            if (m_Text != null)
                m_Text.text = closeText;
            if (m_Image != null)
                m_Image.sprite = closeSprite;
        }
    }

    public void OnClick()
    {
        m_ValidateEventArgs.cancel = false;
        m_OnValidate?.Invoke(m_ValidateEventArgs);
        //外部取消了点击事件的处理
        if (m_ValidateEventArgs.cancel)
            return;
        m_State = (m_State == ButtonState.Open) ? ButtonState.Close : ButtonState.Open;
        UpdateDisplay();
        m_OnChangedState?.Invoke(this);
    }

    public bool IsOpen
    {
        get { return m_State == ButtonState.Open; }
        set
        {
            m_State = value ? ButtonState.Open : ButtonState.Close;
            UpdateDisplay();
        }
    }
}


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
/// <summary>gine;
/// 多状态开关按钮
/// </summary>
public class UIExpandSwitchButton : MonoBehaviour, IPointerClickHandler
{
    public bool interactable = true;
    [SerializeField]
    private int index;
    [SerializeField]
    private Image m_Image;
    [SerializeField]
    private List<Sprite> swapSprites = new List<Sprite>();
    
    public int Index {
        get { return index; }
        set { ChangeState(value); } 
    }

    [Serializable]
    public class StateChangedEvent : UnityEvent<UIExpandSwitchButton> { }

    [FormerlySerializedAs("onChangedState")]
    [SerializeField]
    private StateChangedEvent m_OnChangedState = new StateChangedEvent();

    public void OnPointerClick(PointerEventData eventData)
    {
        if (!interactable)
            return;
        index = (index + 1) % swapSprites.Count;
        ChangeState(index);
    }

    public void ChangeStateWithoutNotify(int index)
    {
        this.index = index;
        UpdateDisplay();
    }

    private void ChangeState(int index)
    {
        this.index = index;
        UpdateDisplay();
        m_OnChangedState?.Invoke(this);
    }

    private void UpdateDisplay()
    {
        if (index < 0 || index >= swapSprites.Count)
            return;
        Sprite sprite = swapSprites[index];
        m_Image.sprite = sprite;
    }
}


评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容