YIUIToolbarExtender.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace YIUIFramework.Editor
  7. {
  8. [InitializeOnLoad]
  9. public static class YIUIToolbarExtender
  10. {
  11. static int m_toolCount;
  12. static GUIStyle m_commandStyle = null;
  13. private static readonly List<(int order, Action action)> m_LeftToolbarGUI = new List<(int, Action)>();
  14. private static readonly List<(int order, Action action)> m_RightToolbarGUI = new List<(int, Action)>();
  15. /// <summary>
  16. /// 添加左侧工具栏GUI
  17. /// </summary>
  18. /// <param name="action">GUI回调</param>
  19. /// <param name="order">排序优先级,数值越小越靠前</param>
  20. public static void AddLeftToolbarGUI(Action action, int order = 0)
  21. {
  22. m_LeftToolbarGUI.Add((order, action));
  23. m_LeftToolbarGUI.Sort((a, b) => a.order.CompareTo(b.order));
  24. }
  25. /// <summary>
  26. /// 添加右侧工具栏GUI
  27. /// </summary>
  28. /// <param name="action">GUI回调</param>
  29. /// <param name="order">排序优先级,数值越小越靠前</param>
  30. public static void AddRightToolbarGUI(Action action, int order = 0)
  31. {
  32. m_RightToolbarGUI.Add((order, action));
  33. m_RightToolbarGUI.Sort((a, b) => a.order.CompareTo(b.order));
  34. }
  35. /// <summary>
  36. /// 移除左侧工具栏GUI
  37. /// </summary>
  38. public static void RemoveLeftToolbarGUI(Action action)
  39. {
  40. m_LeftToolbarGUI.RemoveAll(item => item.action == action);
  41. }
  42. /// <summary>
  43. /// 移除右侧工具栏GUI
  44. /// </summary>
  45. public static void RemoveRightToolbarGUI(Action action)
  46. {
  47. m_RightToolbarGUI.RemoveAll(item => item.action == action);
  48. }
  49. static YIUIToolbarExtender()
  50. {
  51. Type toolbarType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.Toolbar");
  52. #if UNITY_2019_1_OR_NEWER
  53. string fieldName = "k_ToolCount";
  54. #else
  55. string fieldName = "s_ShownToolIcons";
  56. #endif
  57. FieldInfo toolIcons = toolbarType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  58. #if UNITY_2019_3_OR_NEWER
  59. m_toolCount = toolIcons != null ? ((int)toolIcons.GetValue(null)) : 8;
  60. #elif UNITY_2019_1_OR_NEWER
  61. m_toolCount = toolIcons != null ? ((int) toolIcons.GetValue(null)) : 7;
  62. #elif UNITY_2018_1_OR_NEWER
  63. m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 6;
  64. #else
  65. m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 5;
  66. #endif
  67. YIUIToolbarCallback.OnToolbarGUI = OnGUI;
  68. YIUIToolbarCallback.OnToolbarGUILeft = GUILeft;
  69. YIUIToolbarCallback.OnToolbarGUIRight = GUIRight;
  70. }
  71. #if UNITY_2019_3_OR_NEWER
  72. public const float space = 8;
  73. #else
  74. public const float space = 10;
  75. #endif
  76. public const float largeSpace = 20;
  77. public const float buttonWidth = 32;
  78. public const float dropdownWidth = 80;
  79. #if UNITY_2019_1_OR_NEWER
  80. public const float playPauseStopWidth = 140;
  81. #else
  82. public const float playPauseStopWidth = 100;
  83. #endif
  84. static void OnGUI()
  85. {
  86. if (m_commandStyle == null)
  87. {
  88. m_commandStyle = new GUIStyle("CommandLeft");
  89. }
  90. var screenWidth = EditorGUIUtility.currentViewWidth;
  91. float playButtonsPosition = Mathf.RoundToInt((screenWidth - playPauseStopWidth) / 2);
  92. Rect leftRect = new Rect(0, 0, screenWidth, Screen.height);
  93. leftRect.xMin += space;
  94. leftRect.xMin += buttonWidth * m_toolCount;
  95. #if UNITY_2019_3_OR_NEWER
  96. leftRect.xMin += space;
  97. #else
  98. leftRect.xMin += largeSpace; // Spacing between tools and pivot
  99. #endif
  100. leftRect.xMin += 64 * 2;
  101. leftRect.xMax = playButtonsPosition;
  102. Rect rightRect = new Rect(0, 0, screenWidth, Screen.height);
  103. rightRect.xMin = playButtonsPosition;
  104. rightRect.xMin += m_commandStyle.fixedWidth * 3;
  105. rightRect.xMax = screenWidth;
  106. rightRect.xMax -= space;
  107. rightRect.xMax -= dropdownWidth;
  108. rightRect.xMax -= space;
  109. rightRect.xMax -= dropdownWidth;
  110. #if UNITY_2019_3_OR_NEWER
  111. rightRect.xMax -= space;
  112. #else
  113. rightRect.xMax -= largeSpace; // Spacing between layers and account
  114. #endif
  115. rightRect.xMax -= dropdownWidth;
  116. rightRect.xMax -= space;
  117. rightRect.xMax -= buttonWidth;
  118. rightRect.xMax -= space;
  119. rightRect.xMax -= 78;
  120. leftRect.xMin += space;
  121. leftRect.xMax -= space;
  122. rightRect.xMin += space;
  123. rightRect.xMax -= space;
  124. #if UNITY_2019_3_OR_NEWER
  125. leftRect.y = 4;
  126. leftRect.height = 22;
  127. rightRect.y = 4;
  128. rightRect.height = 22;
  129. #else
  130. leftRect.y = 5;
  131. leftRect.height = 24;
  132. rightRect.y = 5;
  133. rightRect.height = 24;
  134. #endif
  135. if (leftRect.width > 0)
  136. {
  137. GUILayout.BeginArea(leftRect);
  138. GUILayout.BeginHorizontal();
  139. GUILayout.FlexibleSpace();
  140. foreach (var item in m_LeftToolbarGUI)
  141. {
  142. item.action();
  143. }
  144. GUILayout.EndHorizontal();
  145. GUILayout.EndArea();
  146. }
  147. if (rightRect.width > 0)
  148. {
  149. GUILayout.BeginArea(rightRect);
  150. GUILayout.BeginHorizontal();
  151. foreach (var item in m_RightToolbarGUI)
  152. {
  153. item.action();
  154. }
  155. GUILayout.FlexibleSpace();
  156. GUILayout.EndHorizontal();
  157. GUILayout.EndArea();
  158. }
  159. }
  160. public static void GUILeft()
  161. {
  162. GUILayout.BeginHorizontal();
  163. GUILayout.FlexibleSpace();
  164. foreach (var item in m_LeftToolbarGUI)
  165. {
  166. item.action();
  167. }
  168. GUILayout.EndHorizontal();
  169. }
  170. public static void GUIRight()
  171. {
  172. GUILayout.BeginHorizontal();
  173. foreach (var item in m_RightToolbarGUI)
  174. {
  175. item.action();
  176. }
  177. GUILayout.FlexibleSpace();
  178. GUILayout.EndHorizontal();
  179. }
  180. }
  181. }