unity3d ~黑科技~ (轉)給你本身的編輯器窗口添加小鎖圖標!

先給原文磕個頭:https://leahayes.wordpress.com/2013/04/30/adding-the-little-padlock-button-to-your-editorwindow/wordpress

還記得Inspector右上角那個小鎖圖標嗎?
其實咱們本身的窗口也能夠添加它,這都得感謝unity 4.0偷偷添加的一個功能!spa

圖片描述

如今經過一個"IHasCustomMenu"接口你就能夠給本身的EditorWindow右上角添加圖標。code

下面是例子(無節操的照搬了原文):接口

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;
        });
    }
 
}
相關文章
相關標籤/搜索