RedDotBind.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections;
  3. using Sirenix.OdinInspector;
  4. using UnityEngine;
  5. namespace YIUIFramework
  6. {
  7. [AddComponentMenu("YIUIFramework/红点/红点通用绑定 【RedDotBind】")]
  8. public class RedDotBind : MonoBehaviour
  9. {
  10. [SerializeField]
  11. [LabelText("红点枚举")]
  12. [EnableIf("@UIOperationHelper.CommonShowIf()")]
  13. #if UNITY_EDITOR
  14. [ValueDropdown("GetKeyTypesSelectList", DoubleClickToConfirm = true)]
  15. [OnValueChanged("OnKeyValueChanged")]
  16. #endif
  17. private int m_Key;
  18. #if UNITY_EDITOR
  19. private IEnumerable GetKeyTypesSelectList()
  20. {
  21. var showValues = new ValueDropdownList<int>();
  22. var keys = RedDotKeyHelper.GetKeys();
  23. foreach (var key in keys)
  24. {
  25. showValues.Add(RedDotKeyHelper.GetDisplayDesc(key), key);
  26. }
  27. return RedDotKeyHelper.SubDisplayValueDropdownList(showValues);
  28. }
  29. [ShowInInspector]
  30. [NonSerialized]
  31. [Delayed]
  32. [OnValueChanged("OnInputKeyValue")]
  33. [HideInPlayMode]
  34. [LabelText("手动选择")]
  35. public int InputChangeKey; //当枚举多了 下拉菜单不好用的时候/或不想下拉菜单选择的时候可手动输入
  36. private void OnInputKeyValue()
  37. {
  38. if (!RedDotKeyHelper.ContainsKey(InputChangeKey))
  39. {
  40. Debug.LogError($"不存在这个Key 请检查 {InputChangeKey}");
  41. m_Key = 0;
  42. InputChangeKey = 0;
  43. return;
  44. }
  45. m_Key = InputChangeKey;
  46. }
  47. private void OnKeyValueChanged()
  48. {
  49. InputChangeKey = m_Key;
  50. }
  51. #endif
  52. public int Key => m_Key;
  53. [ShowInInspector]
  54. [ReadOnly]
  55. [LabelText("显隐")]
  56. public bool Show { get; private set; }
  57. [ShowInInspector]
  58. [ReadOnly]
  59. [LabelText("数量")]
  60. public int Count { get; private set; }
  61. private void Awake()
  62. {
  63. if (m_Key <= 0)
  64. {
  65. Show = false;
  66. Count = 0;
  67. Refresh();
  68. return;
  69. }
  70. OnDestroy();
  71. RedDotMgr.Inst?.AddChanged(m_Key, OnRedDotChangeHandler);
  72. }
  73. private void OnDestroy()
  74. {
  75. if (m_Key <= 0) return;
  76. if (YIUISingletonHelper.Disposing) return;
  77. RedDotMgr.Inst?.RemoveChanged(m_Key, OnRedDotChangeHandler);
  78. }
  79. private void OnRedDotChangeHandler(int count)
  80. {
  81. Show = count >= 1;
  82. Count = count;
  83. Refresh();
  84. }
  85. private void Refresh()
  86. {
  87. gameObject.SetActive(Show);
  88. ChangeText();
  89. }
  90. protected virtual void ChangeText()
  91. {
  92. }
  93. //需要动态改变的,可以预制时,选无
  94. //然后调用这个方法动态的修改
  95. public void ChangeBind(int key, bool force = false)
  96. {
  97. if (key <= 0)
  98. {
  99. Debug.LogError($"修改为 {key} ,尝试修改的Key 错误 不可能 <= 0");
  100. return;
  101. }
  102. if (m_Key == key)
  103. {
  104. return;
  105. }
  106. if (m_Key > 0 && !force)
  107. {
  108. Debug.LogError($"已经有监听的红点 {m_Key} ,想修改为 {key} ,但是当前是非强制修改 所以禁止修改 请检查原因");
  109. return;
  110. }
  111. OnDestroy();
  112. m_Key = key;
  113. Awake();
  114. }
  115. }
  116. }