unity 在toolbar处添加按钮

Posted 三页菌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 在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("热重载", EditorGUIUtility.FindTexture("PlayButton"))))
        
            Debug.Log("HotReload");
            BuildAssemblieEditor.BuildLogic();
        

        GUILayout.EndHorizontal();
    

 

以上是关于unity 在toolbar处添加按钮的主要内容,如果未能解决你的问题,请参考以下文章

怎么在toolbar上添加按钮

按下 ToolBar 按钮时向 UIViewController 添加和删除子 ViewController

QtDesigner怎么添加工具栏toolBar

在 DetailViewController ToolBar for iPad 上添加工具栏项或按钮

VC如何在TOOLBAR上引入Icon或bmp作为按钮图标

Ext面板的toolbar添加按钮没有显示出来