zoukankan      html  css  js  c++  java
  • Unity Editor学习--IHasCustomMenu

    本文章里只是简单地总结各种比较少见的editor使用

    IHasCustomMenu

    Unity中的窗口都可以通过右键窗口的tab 或者 点击窗口右上角的一个菜单按钮 来显示窗口菜单项。窗口都有默认的菜单项。但可以通过实现 IHasCustomMenu 接口中的 AddItemsToMenu 函数 来添加自定义的菜单项。

    public class AssetBundleBrowserMain : EditorWindow, IHasCustomMenu{
        [MenuItem("Window/AssetBundle Browser", priority = 2050)]
        static void ShowWindow()
        {
            var window = GetWindow<AssetBundleBrowserMain>();
            window.titleContent = new GUIContent("AssetBundles");
            window.Show();
        }
        [SerializeField]
        public bool multiDataSource = false;
        public virtual void AddItemsToMenu(GenericMenu menu)
        {
            //menu.AddSeparator(string.Empty);
            menu.AddItem(new GUIContent("Custom Sources"), multiDataSource, FlipDataSource);
        }
        public void FlipDataSource()
        {
            multiDataSource = !multiDataSource;
        }
    }
    

    也可以控制菜单选项的展示方式,比如在菜单上默认显示的锁头

    using UnityEngine;
    using UnityEditor;
     
    public class WindowWithLockIcon : EditorWindow, IHasCustomMenu {
     
        /// <summary>
        /// Menu item to display our test window.
        /// </summary>
        [MenuItem("Window/Window with Lock Icon")]
        static void Show() {
            GetWindow<WindowWithLockIcon>("Lock Test");
        }
     
        /// <summary>
        /// Keep local copy of lock button style for efficiency.
        /// </summary>
        [System.NonSerialized]
        GUIStyle lockButtonStyle;
        /// <summary>
        /// Indicates whether lock is toggled on/off.
        /// </summary>
        [System.NonSerialized]
        bool locked = false;
     
        /// <summary>
        /// Magic method which Unity detects automatically.
        /// </summary>
        /// <param name="position">Position of button.</param>
        void ShowButton(Rect position) {
            if (lockButtonStyle == null)
                lockButtonStyle = "IN LockButton";
            locked = GUI.Toggle(position, locked, GUIContent.none, lockButtonStyle);
        }
     
        /// <summary>
        /// Adds custom items to editor window context menu.
        /// </summary>
        /// <remarks>
        /// <para>This will only work for Unity 4.x+</para>
        /// </remarks>
        /// <param name="menu">Context menu.</param>
        void IHasCustomMenu.AddItemsToMenu(GenericMenu menu) {
            menu.AddItem(new GUIContent("Lock"), locked, () => {
                locked = !locked;
            });
        }
     
    }
    
  • 相关阅读:
    Objective C 绘制透明窗口的方法
    在挂载的 NTFS 盘上运行 gdb 会遇到权限问题,导致无法初始化
    使用 Eclipse 打造 操作系统实习 JOS 开发环境
    C# URL 中文编码与解码
    突破教育网的防线,将搜狗浏览器的教育网加速功能全面推向各种浏览器
    linux 截屏工具
    yum install 时 提示某个已安装的库(x86_64)比某个要安装的库(i686) 新导致安装失败
    全方位打造 Eclipse 自定义开发环境
    自动售饮料机的verilog实现
    数字跑表的verilog实现
  • 原文地址:https://www.cnblogs.com/chiefCTO/p/7121046.html
Copyright © 2011-2022 走看看