├── Editor.meta ├── Editor ├── ToolbarCallback.cs ├── ToolbarCallback.cs.meta ├── ToolbarDefinition.cs ├── ToolbarDefinition.cs.meta ├── ToolbarExtension.Editor.asmdef ├── ToolbarExtension.Editor.asmdef.meta ├── ToolbarExtension.cs └── ToolbarExtension.cs.meta ├── Images~ └── 1.png ├── README.md ├── README.md.meta ├── package.json └── package.json.meta /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d73a4011660ee449cb98925f36be3dac 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/ToolbarCallback.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using System.Reflection; 4 | using UnityEditor; 5 | using UnityEngine.UIElements; 6 | 7 | namespace ToolbarExtension 8 | { 9 | internal static class ToolbarCallback 10 | { 11 | static readonly Type m_toolbarType = typeof(Editor).Assembly.GetType("UnityEditor.Toolbar"); 12 | 13 | 14 | public static Action OnToolbarGUILeft; 15 | public static Action OnToolbarGUIRight; 16 | 17 | private static VisualElement m_ToolBarRootVisualElement; 18 | private static ScriptableObject m_ToolBarScriptableObject; 19 | private static FieldInfo m_ToolbarRootFieldInfo; 20 | 21 | static ToolbarCallback() 22 | { 23 | EditorApplication.update -= OnUpdate; 24 | EditorApplication.update += OnUpdate; 25 | } 26 | 27 | static void RegisterCallback(string root, Action cb) 28 | { 29 | var toolbarZone = m_ToolBarRootVisualElement.Q(root); 30 | var parent = new VisualElement() 31 | { 32 | style = 33 | { 34 | flexGrow = 1, 35 | flexDirection = FlexDirection.Row, 36 | } 37 | }; 38 | var container = new IMGUIContainer(); 39 | container.style.flexGrow = 1; 40 | container.onGUIHandler += () => 41 | { 42 | cb?.Invoke(); 43 | }; 44 | parent.Add(container); 45 | toolbarZone.Add(parent); 46 | } 47 | 48 | static void OnUpdate() 49 | { 50 | if (m_ToolBarRootVisualElement == null) 51 | { 52 | if (m_ToolBarScriptableObject == null) 53 | { 54 | var toolbars = Resources.FindObjectsOfTypeAll(m_toolbarType); 55 | m_ToolBarScriptableObject = toolbars.Length > 0 ? (ScriptableObject)toolbars[0] : null; 56 | } 57 | 58 | if (m_ToolbarRootFieldInfo == null && m_ToolBarScriptableObject != null) 59 | { 60 | m_ToolbarRootFieldInfo = m_ToolBarScriptableObject.GetType() 61 | .GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance); 62 | } 63 | 64 | if (m_ToolbarRootFieldInfo != null) 65 | { 66 | m_ToolBarRootVisualElement = m_ToolbarRootFieldInfo.GetValue(m_ToolBarScriptableObject) as VisualElement; 67 | if (m_ToolBarRootVisualElement != null) 68 | { 69 | RegisterCallback("ToolbarZoneLeftAlign", OnToolbarGUILeft); 70 | RegisterCallback("ToolbarZoneRightAlign", OnToolbarGUIRight); 71 | } 72 | } 73 | } 74 | else 75 | { 76 | VisualElement curRoot = m_ToolbarRootFieldInfo.GetValue(m_ToolBarScriptableObject) as VisualElement; 77 | if (m_ToolBarRootVisualElement != curRoot) 78 | { 79 | m_ToolBarRootVisualElement = curRoot; 80 | RegisterCallback("ToolbarZoneLeftAlign", OnToolbarGUILeft); 81 | RegisterCallback("ToolbarZoneRightAlign", OnToolbarGUIRight); 82 | } 83 | } 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /Editor/ToolbarCallback.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6249d6d81278a6a42a2fb1acc3781543 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/ToolbarDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ToolbarExtension 4 | { 5 | public enum OnGUISide : byte 6 | { 7 | Left, 8 | Right, 9 | } 10 | 11 | [AttributeUsage(AttributeTargets.Method, Inherited = false)] 12 | public sealed class ToolbarAttribute : Attribute 13 | { 14 | public OnGUISide Side { get; } 15 | public int Priority { get; } 16 | 17 | public ToolbarAttribute(OnGUISide side, int priority) 18 | { 19 | Side = side; 20 | Priority = priority; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Editor/ToolbarDefinition.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2101ffc459547c1b59aabc707f59683 3 | timeCreated: 1678791128 -------------------------------------------------------------------------------- /Editor/ToolbarExtension.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ToolbarExtension.Editor", 3 | "rootNamespace": "", 4 | "references": [], 5 | "includePlatforms": [ 6 | "Editor" 7 | ], 8 | "excludePlatforms": [], 9 | "allowUnsafeCode": false, 10 | "overrideReferences": false, 11 | "precompiledReferences": [], 12 | "autoReferenced": true, 13 | "defineConstraints": [], 14 | "versionDefines": [], 15 | "noEngineReferences": false 16 | } -------------------------------------------------------------------------------- /Editor/ToolbarExtension.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a0471484b079be408cb3a0d7fe94736 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor/ToolbarExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEditor; 4 | using UnityEngine; 5 | 6 | namespace ToolbarExtension 7 | { 8 | [InitializeOnLoad] 9 | internal static class ToolbarExtension 10 | { 11 | private static readonly List<(int, Action)> s_LeftToolbarGUI = new List<(int, Action)>(); 12 | private static readonly List<(int, Action)> s_RightToolbarGUI = new List<(int, Action)>(); 13 | 14 | static ToolbarExtension() 15 | { 16 | ToolbarCallback.OnToolbarGUILeft = GUILeft; 17 | ToolbarCallback.OnToolbarGUIRight = GUIRight; 18 | Type attributeType = typeof(ToolbarAttribute); 19 | 20 | foreach (var methodInfo in TypeCache.GetMethodsWithAttribute()) 21 | { 22 | var attributes = methodInfo.GetCustomAttributes(attributeType, false); 23 | if (attributes.Length > 0) 24 | { 25 | ToolbarAttribute attribute = (ToolbarAttribute)attributes[0]; 26 | if (attribute.Side == OnGUISide.Left) 27 | { 28 | s_LeftToolbarGUI.Add((attribute.Priority, delegate 29 | { 30 | methodInfo.Invoke(null, null); 31 | })); 32 | continue; 33 | } 34 | if (attribute.Side == OnGUISide.Right) 35 | { 36 | s_RightToolbarGUI.Add((attribute.Priority, delegate 37 | { 38 | methodInfo.Invoke(null, null); 39 | })); 40 | continue; 41 | } 42 | } 43 | } 44 | s_LeftToolbarGUI.Sort((tuple1, tuple2) => tuple1.Item1 - tuple2.Item1); 45 | s_RightToolbarGUI.Sort((tuple1, tuple2) => tuple2.Item1 - tuple1.Item1); 46 | } 47 | 48 | static void GUILeft() 49 | { 50 | GUILayout.BeginHorizontal(); 51 | GUILayout.FlexibleSpace(); 52 | foreach (var handler in s_LeftToolbarGUI) 53 | { 54 | handler.Item2(); 55 | } 56 | 57 | GUILayout.EndHorizontal(); 58 | } 59 | 60 | static void GUIRight() 61 | { 62 | GUILayout.BeginHorizontal(); 63 | foreach (var handler in s_RightToolbarGUI) 64 | { 65 | handler.Item2(); 66 | } 67 | 68 | GUILayout.FlexibleSpace(); 69 | GUILayout.EndHorizontal(); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Editor/ToolbarExtension.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 98dc2ee9817d16c4e8c71d552b4c7c76 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Images~/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XuToWei/Unity-ToolbarExtension/29b61c47e5ff4ba7f1976918ec0d4b9b4ecb1576/Images~/1.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Toolbar 2 | 3 | 可以扩展Unity Toolbar的工具 4 | 5 | ![](Images~/1.png) 6 | 7 | ### 安装方式: 8 | - Open Package Manager and Add package from git URL https://github.com/XuToWei/ToolbarExtension.git. 9 | 10 | ## 使用说明: 11 | 12 | 在编辑器函数上添加Toolbar特性即可 13 | 14 | ```csharp 15 | [Toolbar(OnGUISide.Left, 0)] 16 | static void OnToolbarGUI() 17 | { 18 | if (GUILayout.Button("Test")) 19 | { 20 | Debug.Log("Test"); 21 | } 22 | } 23 | ``` 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b5eeb47f321bedd4b99e3ff1945dbb80 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "me.xw.toolbarextension", 3 | "version": "1.0.1", 4 | "displayName": "ToolbarExtension", 5 | "description": "better ToolbarExtension", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/XuToWei/ToolbarExtension.git" 9 | }, 10 | "unity": "2019.4", 11 | "documentationUrl": "https://github.com/XuToWei/ToolbarExtension/blob/master/README.md", 12 | "licensesUrl": "https://github.com/XuToWei/ToolbarExtension/blob/master/LICENSE.md", 13 | "dependencies": { 14 | }, 15 | "keywords": [ 16 | ], 17 | "author": { 18 | "name": "Xu Wei", 19 | "email": "", 20 | "url": "" 21 | }, 22 | "samples": [ 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 468cf80569192a04aaae64ffc9d94677 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | --------------------------------------------------------------------------------