RedDotKeyHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Sirenix.OdinInspector;
  5. using Sirenix.Utilities;
  6. namespace YIUIFramework
  7. {
  8. public static class RedDotKeyHelper
  9. {
  10. private static List<int> m_AllRedDotKey;
  11. private static List<int> AllRedDotKey
  12. {
  13. get
  14. {
  15. if (m_AllRedDotKey == null)
  16. {
  17. GetKeys();
  18. }
  19. return m_AllRedDotKey;
  20. }
  21. }
  22. private static HashSet<int> m_HashAllRedDotKey;
  23. /// <summary>
  24. /// 检查这个Key 是否存在
  25. /// </summary>
  26. public static bool ContainsKey(int key)
  27. {
  28. if (m_HashAllRedDotKey == null)
  29. {
  30. m_HashAllRedDotKey = new();
  31. m_HashAllRedDotKey.AddRange(AllRedDotKey);
  32. }
  33. return m_HashAllRedDotKey.Contains(key);
  34. }
  35. /// <summary>
  36. /// 反射获取枚举列表
  37. /// 注意: 运行时使用的是ET编译过的dll
  38. /// 如果新增了没有编译 则无法获取到最新数据 请注意一定要编译后运行
  39. /// </summary>
  40. public static List<int> GetKeys(bool force = false)
  41. {
  42. if (m_AllRedDotKey != null && !force)
  43. {
  44. return m_AllRedDotKey;
  45. }
  46. m_AllRedDotKey = new();
  47. m_HashAllRedDotKey = null;
  48. #if UNITY_EDITOR
  49. m_RedDotKeyDesc = new();
  50. #endif
  51. var assembly = AssemblyHelper.GetAssembly("ET.Model");
  52. if (assembly == null)
  53. {
  54. Logger.LogError($"没有找到ET.Model程序集");
  55. return m_AllRedDotKey;
  56. }
  57. Type redDotKeyType = assembly.GetType("ET.ERedDotKeyType");
  58. if (redDotKeyType == null)
  59. {
  60. Logger.LogError($"没有找到ET.ERedDotKeyType类型");
  61. return m_AllRedDotKey;
  62. }
  63. FieldInfo[] fields = redDotKeyType.GetFields(BindingFlags.Public | BindingFlags.Static);
  64. foreach (FieldInfo field in fields)
  65. {
  66. if (field.IsLiteral && field.FieldType == typeof(int))
  67. {
  68. int key = (int)field.GetValue(null);
  69. m_AllRedDotKey.Add(key);
  70. #if UNITY_EDITOR
  71. var keyDesc = "";
  72. var labelTextAttribute = field.GetCustomAttribute<LabelTextAttribute>();
  73. if (labelTextAttribute != null)
  74. {
  75. keyDesc = labelTextAttribute.Text;
  76. }
  77. m_RedDotKeyDesc[key] = keyDesc;
  78. #endif
  79. }
  80. }
  81. return m_AllRedDotKey;
  82. }
  83. #if UNITY_EDITOR
  84. private static Dictionary<int, string> m_RedDotKeyDesc;
  85. // 获取红点描述
  86. // Editor 时可用
  87. internal static string GetDesc(int key)
  88. {
  89. return m_RedDotKeyDesc?.GetValueOrDefault(key, "");
  90. }
  91. // 统一显示红点描述
  92. internal static string GetDisplayDesc(int key)
  93. {
  94. return $"{key}_{m_RedDotKeyDesc?.GetValueOrDefault(key, "")}";
  95. }
  96. private const int MAX_GROUP_SIZE = 10;
  97. // 显示红点描述的下拉列表 超过10个时分组显示
  98. internal static ValueDropdownList<int> SubDisplayValueDropdownList(ValueDropdownList<int> keys)
  99. {
  100. //排序
  101. keys.Sort((a, b) =>
  102. {
  103. var aId = a.Value;
  104. var bId = b.Value;
  105. return aId.CompareTo(bId);
  106. });
  107. if (keys.Count <= MAX_GROUP_SIZE)
  108. {
  109. return keys;
  110. }
  111. return GetGroupValueDropdownList(keys, MAX_GROUP_SIZE);
  112. }
  113. private static ValueDropdownList<int> GetGroupValueDropdownList(ValueDropdownList<int> numbers, int groupSize)
  114. {
  115. ValueDropdownList<int> allGroups = new ValueDropdownList<int>();
  116. int count = numbers.Count;
  117. while (count > 0)
  118. {
  119. int groupCount = Math.Min(groupSize, count);
  120. var startItem = numbers[0];
  121. var startItemId = startItem.Value;
  122. var endItem = numbers[groupCount - 1];
  123. var endItemId = endItem.Value;
  124. var groupText = $"{startItemId} - {endItemId} ";
  125. for (int i = 0; i < groupCount; i++)
  126. {
  127. var item = numbers[0];
  128. var newItem = new ValueDropdownItem<int>();
  129. newItem.Text = $"{groupText}/{item.Text}";
  130. newItem.Value = item.Value;
  131. allGroups.Add(newItem);
  132. numbers.RemoveAt(0);
  133. }
  134. count -= groupCount;
  135. }
  136. int nextGroupSize = groupSize * MAX_GROUP_SIZE;
  137. if (allGroups.Count > nextGroupSize)
  138. {
  139. return GetGroupValueDropdownList(allGroups, nextGroupSize);
  140. }
  141. return allGroups;
  142. }
  143. #endif
  144. }
  145. }