ToolbarExtender.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace UnityToolbarExtender
  7. {
  8. public interface INavigation
  9. {
  10. void OnGUI();
  11. int Order { get; }
  12. }
  13. [InitializeOnLoad]
  14. public static class ToolbarExtender
  15. {
  16. static int m_toolCount;
  17. static GUIStyle m_commandStyle = null;
  18. static readonly List<INavigation> LeftToolbarGUI = new List<INavigation>();
  19. static readonly List<INavigation> RightToolbarGUI = new List<INavigation>();
  20. /// <summary>
  21. /// Ìí¼Óµ½×ó±ß
  22. /// </summary>
  23. /// <param name="navigation"></param>
  24. static public void AddToLeft(INavigation navigation)
  25. {
  26. if (navigation != null)
  27. {
  28. if (!LeftToolbarGUI.Contains(navigation))
  29. LeftToolbarGUI.Add(navigation);
  30. LeftToolbarGUI.Sort((a, b) => b.Order - a.Order);
  31. }
  32. }
  33. /// <summary>
  34. /// Ìí¼Óµ½ÓÒ±ß
  35. /// </summary>
  36. /// <param name="navigation"></param>
  37. static public void AddToRight(INavigation navigation)
  38. {
  39. if (navigation != null)
  40. {
  41. if (!RightToolbarGUI.Contains(navigation))
  42. RightToolbarGUI.Add(navigation);
  43. RightToolbarGUI.Sort((a, b) => b.Order - a.Order);
  44. }
  45. }
  46. static ToolbarExtender()
  47. {
  48. Type toolbarType = typeof(Editor).Assembly.GetType("UnityEditor.Toolbar");
  49. #if UNITY_2019_1_OR_NEWER
  50. string fieldName = "k_ToolCount";
  51. #else
  52. string fieldName = "s_ShownToolIcons";
  53. #endif
  54. FieldInfo toolIcons = toolbarType.GetField(fieldName,
  55. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  56. #if UNITY_2019_3_OR_NEWER
  57. m_toolCount = toolIcons != null ? ((int)toolIcons.GetValue(null)) : 8;
  58. #elif UNITY_2019_1_OR_NEWER
  59. m_toolCount = toolIcons != null ? ((int) toolIcons.GetValue(null)) : 7;
  60. #elif UNITY_2018_1_OR_NEWER
  61. m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 6;
  62. #else
  63. m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 5;
  64. #endif
  65. ToolbarCallback.OnToolbarGUI = OnGUI;
  66. ToolbarCallback.OnToolbarGUILeft = GUILeft;
  67. ToolbarCallback.OnToolbarGUIRight = GUIRight;
  68. }
  69. #if UNITY_2019_3_OR_NEWER
  70. public const float space = 12;
  71. #else
  72. public const float space = 14;
  73. #endif
  74. public const float largeSpace = 20;
  75. public const float buttonWidth = 32;
  76. public const float dropdownWidth = 80;
  77. #if UNITY_2019_1_OR_NEWER
  78. public const float playPauseStopWidth = 140;
  79. #else
  80. public const float playPauseStopWidth = 100;
  81. #endif
  82. static void OnGUI()
  83. {
  84. // Create two containers, left and right
  85. // Screen is whole toolbar
  86. if (m_commandStyle == null)
  87. {
  88. m_commandStyle = new GUIStyle("CommandLeft");
  89. }
  90. var screenWidth = EditorGUIUtility.currentViewWidth;
  91. // Following calculations match code reflected from Toolbar.OldOnGUI()
  92. float playButtonsPosition = Mathf.RoundToInt((screenWidth - playPauseStopWidth) / 2);
  93. Rect leftRect = new Rect(0, 0, screenWidth, Screen.height);
  94. leftRect.xMin += space; // Spacing left
  95. leftRect.xMin += buttonWidth * m_toolCount; // Tool buttons
  96. #if UNITY_2019_3_OR_NEWER
  97. leftRect.xMin += space; // Spacing between tools and pivot
  98. #else
  99. leftRect.xMin += largeSpace; // Spacing between tools and pivot
  100. #endif
  101. leftRect.xMin += 64 * 2; // Pivot buttons
  102. leftRect.xMax = playButtonsPosition;
  103. Rect rightRect = new Rect(0, 0, screenWidth, Screen.height);
  104. rightRect.xMin = playButtonsPosition;
  105. rightRect.xMin += m_commandStyle.fixedWidth * 3; // Play buttons
  106. rightRect.xMax = screenWidth;
  107. rightRect.xMax -= space; // Spacing right
  108. rightRect.xMax -= dropdownWidth; // Layout
  109. rightRect.xMax -= space; // Spacing between layout and layers
  110. rightRect.xMax -= dropdownWidth; // Layers
  111. #if UNITY_2019_3_OR_NEWER
  112. rightRect.xMax -= space; // Spacing between layers and account
  113. #else
  114. rightRect.xMax -= largeSpace; // Spacing between layers and account
  115. #endif
  116. rightRect.xMax -= dropdownWidth; // Account
  117. rightRect.xMax -= space; // Spacing between account and cloud
  118. rightRect.xMax -= buttonWidth; // Cloud
  119. rightRect.xMax -= space; // Spacing between cloud and collab
  120. rightRect.xMax -= 78; // Colab
  121. // Add spacing around existing controls
  122. leftRect.xMin += space;
  123. leftRect.xMax -= space;
  124. rightRect.xMin += space;
  125. rightRect.xMax -= space;
  126. // Add top and bottom margins
  127. #if UNITY_2019_3_OR_NEWER
  128. leftRect.y = 4;
  129. leftRect.height = 22;
  130. rightRect.y = 4;
  131. rightRect.height = 22;
  132. #else
  133. leftRect.y = 5;
  134. leftRect.height = 24;
  135. rightRect.y = 5;
  136. rightRect.height = 24;
  137. #endif
  138. if (leftRect.width > 0)
  139. {
  140. GUILayout.BeginArea(leftRect);
  141. GUILayout.BeginHorizontal();
  142. foreach (var navigation in LeftToolbarGUI)
  143. navigation.OnGUI();
  144. GUILayout.EndHorizontal();
  145. GUILayout.EndArea();
  146. }
  147. if (rightRect.width > 0)
  148. {
  149. GUILayout.BeginArea(rightRect);
  150. GUILayout.BeginHorizontal();
  151. foreach (var navigation in RightToolbarGUI)
  152. navigation.OnGUI();
  153. GUILayout.EndHorizontal();
  154. GUILayout.EndArea();
  155. }
  156. }
  157. public static void GUILeft()
  158. {
  159. GUILayout.BeginHorizontal();
  160. foreach (var navigation in LeftToolbarGUI)
  161. navigation.OnGUI();
  162. GUILayout.EndHorizontal();
  163. }
  164. public static void GUIRight()
  165. {
  166. GUILayout.BeginHorizontal();
  167. foreach (var navigation in RightToolbarGUI)
  168. navigation.OnGUI();
  169. GUILayout.EndHorizontal();
  170. }
  171. }
  172. }