123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- namespace UnityToolbarExtender
- {
- public interface INavigation
- {
- void OnGUI();
- int Order { get; }
- }
- [InitializeOnLoad]
- public static class ToolbarExtender
- {
- static int m_toolCount;
- static GUIStyle m_commandStyle = null;
- static readonly List<INavigation> LeftToolbarGUI = new List<INavigation>();
- static readonly List<INavigation> RightToolbarGUI = new List<INavigation>();
- /// <summary>
- /// Ìí¼Óµ½×ó±ß
- /// </summary>
- /// <param name="navigation"></param>
- static public void AddToLeft(INavigation navigation)
- {
- if (navigation != null)
- {
- if (!LeftToolbarGUI.Contains(navigation))
- LeftToolbarGUI.Add(navigation);
- LeftToolbarGUI.Sort((a, b) => b.Order - a.Order);
- }
- }
- /// <summary>
- /// Ìí¼Óµ½ÓÒ±ß
- /// </summary>
- /// <param name="navigation"></param>
- static public void AddToRight(INavigation navigation)
- {
- if (navigation != null)
- {
- if (!RightToolbarGUI.Contains(navigation))
- RightToolbarGUI.Add(navigation);
- RightToolbarGUI.Sort((a, b) => b.Order - a.Order);
- }
- }
- static ToolbarExtender()
- {
- Type toolbarType = typeof(Editor).Assembly.GetType("UnityEditor.Toolbar");
- #if UNITY_2019_1_OR_NEWER
- string fieldName = "k_ToolCount";
- #else
- string fieldName = "s_ShownToolIcons";
- #endif
- FieldInfo toolIcons = toolbarType.GetField(fieldName,
- BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
- #if UNITY_2019_3_OR_NEWER
- m_toolCount = toolIcons != null ? ((int)toolIcons.GetValue(null)) : 8;
- #elif UNITY_2019_1_OR_NEWER
- m_toolCount = toolIcons != null ? ((int) toolIcons.GetValue(null)) : 7;
- #elif UNITY_2018_1_OR_NEWER
- m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 6;
- #else
- m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 5;
- #endif
- ToolbarCallback.OnToolbarGUI = OnGUI;
- ToolbarCallback.OnToolbarGUILeft = GUILeft;
- ToolbarCallback.OnToolbarGUIRight = GUIRight;
- }
- #if UNITY_2019_3_OR_NEWER
- public const float space = 12;
- #else
- public const float space = 14;
- #endif
- public const float largeSpace = 20;
- public const float buttonWidth = 32;
- public const float dropdownWidth = 80;
- #if UNITY_2019_1_OR_NEWER
- public const float playPauseStopWidth = 140;
- #else
- public const float playPauseStopWidth = 100;
- #endif
- static void OnGUI()
- {
- // Create two containers, left and right
- // Screen is whole toolbar
- if (m_commandStyle == null)
- {
- m_commandStyle = new GUIStyle("CommandLeft");
- }
- var screenWidth = EditorGUIUtility.currentViewWidth;
- // Following calculations match code reflected from Toolbar.OldOnGUI()
- float playButtonsPosition = Mathf.RoundToInt((screenWidth - playPauseStopWidth) / 2);
- Rect leftRect = new Rect(0, 0, screenWidth, Screen.height);
- leftRect.xMin += space; // Spacing left
- leftRect.xMin += buttonWidth * m_toolCount; // Tool buttons
- #if UNITY_2019_3_OR_NEWER
- leftRect.xMin += space; // Spacing between tools and pivot
- #else
- leftRect.xMin += largeSpace; // Spacing between tools and pivot
- #endif
- leftRect.xMin += 64 * 2; // Pivot buttons
- leftRect.xMax = playButtonsPosition;
- Rect rightRect = new Rect(0, 0, screenWidth, Screen.height);
- rightRect.xMin = playButtonsPosition;
- rightRect.xMin += m_commandStyle.fixedWidth * 3; // Play buttons
- rightRect.xMax = screenWidth;
- rightRect.xMax -= space; // Spacing right
- rightRect.xMax -= dropdownWidth; // Layout
- rightRect.xMax -= space; // Spacing between layout and layers
- rightRect.xMax -= dropdownWidth; // Layers
- #if UNITY_2019_3_OR_NEWER
- rightRect.xMax -= space; // Spacing between layers and account
- #else
- rightRect.xMax -= largeSpace; // Spacing between layers and account
- #endif
- rightRect.xMax -= dropdownWidth; // Account
- rightRect.xMax -= space; // Spacing between account and cloud
- rightRect.xMax -= buttonWidth; // Cloud
- rightRect.xMax -= space; // Spacing between cloud and collab
- rightRect.xMax -= 78; // Colab
- // Add spacing around existing controls
- leftRect.xMin += space;
- leftRect.xMax -= space;
- rightRect.xMin += space;
- rightRect.xMax -= space;
- // Add top and bottom margins
- #if UNITY_2019_3_OR_NEWER
- leftRect.y = 4;
- leftRect.height = 22;
- rightRect.y = 4;
- rightRect.height = 22;
- #else
- leftRect.y = 5;
- leftRect.height = 24;
- rightRect.y = 5;
- rightRect.height = 24;
- #endif
- if (leftRect.width > 0)
- {
- GUILayout.BeginArea(leftRect);
- GUILayout.BeginHorizontal();
- foreach (var navigation in LeftToolbarGUI)
- navigation.OnGUI();
- GUILayout.EndHorizontal();
- GUILayout.EndArea();
- }
- if (rightRect.width > 0)
- {
- GUILayout.BeginArea(rightRect);
- GUILayout.BeginHorizontal();
- foreach (var navigation in RightToolbarGUI)
- navigation.OnGUI();
- GUILayout.EndHorizontal();
- GUILayout.EndArea();
- }
- }
- public static void GUILeft()
- {
- GUILayout.BeginHorizontal();
- foreach (var navigation in LeftToolbarGUI)
- navigation.OnGUI();
- GUILayout.EndHorizontal();
- }
- public static void GUIRight()
- {
- GUILayout.BeginHorizontal();
- foreach (var navigation in RightToolbarGUI)
- navigation.OnGUI();
- GUILayout.EndHorizontal();
- }
- }
- }
|