UGUI—UITabBar

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

游戏界面导航栏菜单

UITabBar.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.Serialization;
  6. /// <summary>
  7. /// 选项卡导航栏
  8. /// </summary>
  9. public class UITabBar : MonoBehaviour
  10. {
  11. //默认选项项
  12. [SerializeField]
  13. private int defaultTabIndex;
  14. [SerializeField]
  15. private List<UITabButton> tabList = new List<UITabButton>();
  16. [NonSerialized]
  17. public bool canceled;
  18.  
  19. private UITabButton prebutton;
  20. private UITabButton button;
  21.  
  22. //切换时的有效性检测事件
  23. //外部通过设置canceled为true可取消切换操作
  24. [Serializable]
  25. public class ValidityCheckEvent : UnityEvent<UITabBar, int> { }
  26. [FormerlySerializedAs("onValidityCheck")]
  27. [SerializeField]
  28. private ValidityCheckEvent m_OnValidityCheck = new ValidityCheckEvent();
  29.  
  30. //切换选项事件
  31. [Serializable]
  32. public class ChangedEvent : UnityEvent<UITabBar, int> { }
  33. [FormerlySerializedAs("onChanged")]
  34. [SerializeField]
  35. private ChangedEvent m_OnChanged = new ChangedEvent();
  36.  
  37. private void Awake()
  38. {
  39. ChangeTabButton(defaultTabIndex);
  40. }
  41.  
  42. // 获取UITabButton
  43. public UITabButton GetTabButton(int index)
  44. {
  45. UITabButton tab=null;
  46. foreach(var btn in tabList)
  47. {
  48. if (btn.Index == index)
  49. {
  50. tab = btn;
  51. break;
  52. }
  53. }
  54. return tab;
  55. }
  56.  
  57. // 强制切换选项
  58. public void ChangeTabButton(int index)
  59. {
  60. UITabButton tab = GetTabButton(index);
  61. if (tab == prebutton)
  62. return;
  63. prebutton?.SetState(UITabButton.StateEnum.Normal);
  64. tab?.SetState(UITabButton.StateEnum.Selected);
  65. prebutton = tab;
  66. }
  67.  
  68. // 设置红点提示
  69. public void SetDotMark(int index, bool value)
  70. {
  71. UITabButton tab = GetTabButton(index);
  72. tab.SetDotMark(value);
  73. }
  74.  
  75. // 切换检测,由UITabButton调用
  76. public void ValidityCheck(UITabButton button)
  77. {
  78. if (prebutton == button)
  79. return;
  80.  
  81. this.canceled = false;
  82. this.button = button;
  83. //调用外部切换校验函数
  84. //如果外部不希望切换可设置canceled=true
  85. m_OnValidityCheck?.Invoke(this, button.Index);
  86.  
  87. if (!canceled)
  88. {
  89. prebutton?.SetState(UITabButton.StateEnum.Normal);
  90. this.button.SetState(UITabButton.StateEnum.Selected);
  91. prebutton = button;
  92. m_OnChanged?.Invoke(this, button.Index);
  93. }
  94. }
  95. }


UITabButton.cs

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. /// <summary>
  5. /// 选项卡按钮
  6. /// </summary>
  7. public class UITabButton : MonoBehaviour, IPointerClickHandler
  8. {
  9. [SerializeField]
  10. private int m_Index;
  11. [SerializeField]
  12. private Image buttonImage;
  13. [SerializeField]
  14. private Sprite selectedSprite;
  15. [SerializeField]
  16. private Sprite normalSprite;
  17. [SerializeField]
  18. private Sprite disabledSprite;
  19. [SerializeField]
  20. private Image dotImage;//红点提示
  21. [SerializeField]
  22. private Text m_Text;
  23. [SerializeField]
  24. private Color textNormalColor = Color.white;
  25. [SerializeField]
  26. private Color textSelectedColor = Color.white;
  27. [SerializeField]
  28. private Color textDisabledColor = Color.white;
  29. [SerializeField]
  30. private GameObject panel;
  31. [SerializeField]
  32. private UITabBar tabBar;
  33. [SerializeField]
  34. private StateEnum m_State;
  35.  
  36. public enum StateEnum
  37. {
  38. Normal,
  39. Selected,
  40. Disabled
  41. }
  42.  
  43. public int Index { get { return m_Index; } }
  44.  
  45. private void Awake()
  46. {
  47. UpdateDisplay();
  48. }
  49.  
  50. public void OnPointerClick(PointerEventData eventData)
  51. {
  52. Debug.Log(gameObject.name);
  53. tabBar.ValidityCheck(this);
  54. }
  55.  
  56. // 更新显示
  57. private void UpdateDisplay()
  58. {
  59. switch(m_State)
  60. {
  61. case StateEnum.Normal:
  62. buttonImage.enabled = (normalSprite != null);
  63. buttonImage.sprite = normalSprite;
  64. if (m_Text != null)
  65. {
  66. m_Text.color = textNormalColor;
  67. m_Text.SetAllDirty();
  68. }
  69. if (panel != null)
  70. panel?.SetActive(false);
  71. break;
  72. case StateEnum.Selected:
  73. buttonImage.enabled = (selectedSprite != null);
  74. buttonImage.sprite = selectedSprite;
  75. if (m_Text != null)
  76. m_Text.color = textSelectedColor;
  77. if (panel != null)
  78. panel?.SetActive(true);
  79. break;
  80. case StateEnum.Disabled:
  81. buttonImage.enabled = (disabledSprite != null);
  82. buttonImage.sprite = disabledSprite;
  83. if (m_Text != null)
  84. m_Text.color = textDisabledColor;
  85. if (panel != null)
  86. panel?.SetActive(false);
  87. break;
  88. }
  89. }
  90.  
  91. // 设置按钮状态
  92. public void SetState(StateEnum state)
  93. {
  94. m_State = state;
  95. UpdateDisplay();
  96. }
  97.  
  98. // 设置红点提示
  99. public void SetDotMark(bool value)
  100. {
  101. dotImage?.gameObject.SetActive(value);
  102. }
  103. }


运行效果

11118.gif

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号