1

先给原文磕个头:https://leahayes.wordpress.com/2013/04/30/adding-the-little-padlock-button-to-your-editorwindow/

还记得Inspector右上角那个小锁图标吗?
其实我们自己的窗口也可以添加它,这都得感谢unity 4.0偷偷添加的一个功能!

图片描述

现在通过一个"IHasCustomMenu"接口你就可以给自己的EditorWindow右上角添加图标。

下面是例子(无节操的照搬了原文):

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;
        });
    }
 
}

迷途吧
145 声望25 粉丝

看见好文章就翻译一下,发现一些细节就分享一下,也许没什么技术,只希望能被需要的人搜到。


引用和评论

0 条评论