首页 > 其他分享 >unity 在toolbar处添加按钮

unity 在toolbar处添加按钮

时间:2023-03-19 11:45:04浏览次数:38  
标签:UnityEngine sCurrentToolbar private VisualElement unity static 按钮 using toolbar

 

 

using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

[InitializeOnLoad]
public static class CruToolbar
{
    private static readonly Type kToolbarType = typeof(Editor).Assembly.GetType("UnityEditor.Toolbar");
    private static ScriptableObject sCurrentToolbar;
    
    static CruToolbar()
    {
        EditorApplication.update += OnUpdate;
    }

    private static void OnUpdate()
    {
        if (sCurrentToolbar == null)
        {
            UnityEngine.Object[] toolbars = Resources.FindObjectsOfTypeAll(kToolbarType);
            sCurrentToolbar = toolbars.Length > 0 ? (ScriptableObject) toolbars[0] : null;
            if (sCurrentToolbar != null)
            {
                FieldInfo root = sCurrentToolbar.GetType()
                    .GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance);
                VisualElement concreteRoot = root.GetValue(sCurrentToolbar) as VisualElement;

                VisualElement toolbarZone = concreteRoot.Q("ToolbarZoneRightAlign");
                VisualElement parent = new VisualElement()
                {
                    style =
                    {
                        flexGrow = 1,
                        flexDirection = FlexDirection.Row,
                    }
                };
                IMGUIContainer container = new IMGUIContainer();
                container.onGUIHandler += OnGuiBody;
                parent.Add(container);
                toolbarZone.Add(parent);
            }
        }
    }

    private static void OnGuiBody()
    {
        //自定义按钮加在此处
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(new GUIContent("热重载")))
        {
            Debug.Log("HotReload");
        }

        GUILayout.EndHorizontal();
    }
}

 

标签:UnityEngine,sCurrentToolbar,private,VisualElement,unity,static,按钮,using,toolbar
From: https://www.cnblogs.com/sanyejun/p/17232698.html

相关文章