UGUI—UITabBar

作者:追风剑情 发布于:2022-8-3 18:04

游戏界面导航栏菜单

UITabBar.cs

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;
/// <summary>
/// 选项卡导航栏
/// </summary>
public class UITabBar : MonoBehaviour
{
    //默认选项项
    [SerializeField]
    private int defaultTabIndex;
    [SerializeField]
    private List<UITabButton> tabList = new List<UITabButton>();
    [NonSerialized]
    public bool canceled;

    private UITabButton prebutton;
    private UITabButton button;

    //切换时的有效性检测事件
    //外部通过设置canceled为true可取消切换操作
    [Serializable]
    public class ValidityCheckEvent : UnityEvent<UITabBar, int> { }
    [FormerlySerializedAs("onValidityCheck")]
    [SerializeField]
    private ValidityCheckEvent m_OnValidityCheck = new ValidityCheckEvent();

    //切换选项事件
    [Serializable]
    public class ChangedEvent : UnityEvent<UITabBar, int> { }
    [FormerlySerializedAs("onChanged")]
    [SerializeField]
    private ChangedEvent m_OnChanged = new ChangedEvent();

    private void Awake()
    {
        ChangeTabButton(defaultTabIndex);
    }

    // 获取UITabButton
    public UITabButton GetTabButton(int index)
    {
        UITabButton tab=null;
        foreach(var btn in tabList)
        {
            if (btn.Index == index)
            {
                tab = btn;
                break;
            }
        }
        return tab;
    }

    // 强制切换选项
    public void ChangeTabButton(int index)
    {
        UITabButton tab = GetTabButton(index);
        if (tab == prebutton)
            return;
        prebutton?.SetState(UITabButton.StateEnum.Normal);
        tab?.SetState(UITabButton.StateEnum.Selected);
        prebutton = tab;
    }

    // 设置红点提示
    public void SetDotMark(int index, bool value)
    {
        UITabButton tab = GetTabButton(index);
        tab.SetDotMark(value);
    }

    // 切换检测,由UITabButton调用
    public void ValidityCheck(UITabButton button)
    {
        if (prebutton == button)
            return;

        this.canceled = false;
        this.button = button;
        //调用外部切换校验函数
        //如果外部不希望切换可设置canceled=true
        m_OnValidityCheck?.Invoke(this, button.Index);

        if (!canceled)
        {
            prebutton?.SetState(UITabButton.StateEnum.Normal);
            this.button.SetState(UITabButton.StateEnum.Selected);
            prebutton = button;
            m_OnChanged?.Invoke(this, button.Index);
        }
    }
}


UITabButton.cs

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// 选项卡按钮
/// </summary>
public class UITabButton : MonoBehaviour, IPointerClickHandler
{
    [SerializeField]
    private int m_Index;
    [SerializeField]
    private Image buttonImage;
    [SerializeField]
    private Sprite selectedSprite;
    [SerializeField]
    private Sprite normalSprite;
    [SerializeField]
    private Sprite disabledSprite;
    [SerializeField]
    private Image dotImage;//红点提示
    [SerializeField]
    private Text m_Text;
    [SerializeField]
    private Color textNormalColor = Color.white;
    [SerializeField]
    private Color textSelectedColor = Color.white;
    [SerializeField]
    private Color textDisabledColor = Color.white;
    [SerializeField]
    private GameObject panel;
    [SerializeField]
    private UITabBar tabBar;
    [SerializeField]
    private StateEnum m_State;

    public enum StateEnum
    {
        Normal,
        Selected,
        Disabled
    }

    public int Index { get { return m_Index; } }

    private void Awake()
    {
        UpdateDisplay();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log(gameObject.name);
        tabBar.ValidityCheck(this);
    }

    // 更新显示
    private void UpdateDisplay()
    {
        switch(m_State)
        {
            case StateEnum.Normal:
                buttonImage.enabled = (normalSprite != null);
                buttonImage.sprite = normalSprite;
                if (m_Text != null)
                {
                    m_Text.color = textNormalColor;
                    m_Text.SetAllDirty();
                }
                if (panel != null)
                    panel?.SetActive(false);
                break;
            case StateEnum.Selected:
                buttonImage.enabled = (selectedSprite != null);
                buttonImage.sprite = selectedSprite;
                if (m_Text != null)
                    m_Text.color = textSelectedColor;
                if (panel != null)
                    panel?.SetActive(true);
                break;
            case StateEnum.Disabled:
                buttonImage.enabled = (disabledSprite != null);
                buttonImage.sprite = disabledSprite;
                if (m_Text != null)
                    m_Text.color = textDisabledColor;
                if (panel != null)
                    panel?.SetActive(false);
                break;
        }
    }

    // 设置按钮状态
    public void SetState(StateEnum state)
    {
        m_State = state;
        UpdateDisplay();
    }

    // 设置红点提示
    public void SetDotMark(bool value)
    {
        dotImage?.gameObject.SetActive(value);
    }
}


运行效果

11118.gif

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号