com.alicizax.unity.editor.e.../Editor/ToolBarExtension/EditorQuickToolBar.cs

63 lines
2.0 KiB
C#
Raw Normal View History

2025-03-11 17:46:52 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Paps.UnityToolbarExtenderUIToolkit;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace AlicizaX.Editor.Extension
{
2026-04-07 17:06:12 +08:00
[Paps.UnityToolbarExtenderUIToolkit.MainToolbarElement("EditorQuickToolBar", alignment: Paps.UnityToolbarExtenderUIToolkit.ToolbarAlign.Right, order: 1)]
2025-03-11 17:46:52 +08:00
public class EditorQuickToolBar : IMGUIContainer
{
2025-12-01 16:45:54 +08:00
private GUIContent toolsDropBtContent;
2025-03-11 17:46:52 +08:00
public void InitializeElement()
{
toolsDropBtContent = EditorGUIUtility.TrTextContentWithIcon("Tools", "工具箱", "CustomTool");
onGUIHandler = MyGUIMethod;
}
private void MyGUIMethod()
{
GUILayout.BeginHorizontal();
if (EditorGUILayout.DropdownButton(toolsDropBtContent, FocusType.Passive, EditorStyles.toolbarPopup,
GUILayout.MaxWidth(90)))
{
DrawEditorToolDropdownMenus();
}
GUILayout.Space(5);
GUILayout.EndHorizontal();
}
2025-12-01 16:45:54 +08:00
void DrawEditorToolDropdownMenus()
2025-03-11 17:46:52 +08:00
{
GenericMenu popMenu = new GenericMenu();
for (int i = 0; i < EditorToolFunctionAttributeCollector.Attributes.Count; i++)
{
var toolAttr = EditorToolFunctionAttributeCollector.Attributes[i];
popMenu.AddItem(new GUIContent(toolAttr.ToolMenuPath), false,
menuIdx => { ClickToolsSubmenu((int)menuIdx); }, i);
}
popMenu.ShowAsContext();
}
2025-12-01 16:45:54 +08:00
void ClickToolsSubmenu(int menuIdx)
2025-03-11 17:46:52 +08:00
{
var editorTp = EditorToolFunctionAttributeCollector.Attributes[menuIdx];
if (editorTp.MethodInfo != null && editorTp.MethodInfo.IsStatic)
{
2025-10-11 15:16:57 +08:00
editorTp.MethodInfo.Invoke(null, null);
2025-03-11 17:46:52 +08:00
}
else
{
Debug.LogError("Method is not static or not found.");
}
}
}
}