Unity Editor 编辑器扩展 六 EditorWindow

自闭症网瘾萝莉.ら 提交于 2019-12-03 15:45:25

目录

EditorWindow的简单用法之前都用过了,这里介绍一些特殊的用法

窗口弹出框

在Editor下创建脚本如下:

using UnityEditor;
using UnityEngine;

public class WindowExample1 : EditorWindow
{
    [MenuItem("Window/WindowExample1")]
    static void Open ()
    {
        GetWindow<WindowExample1> ();
    }


    ExamplePupupContent popupContent = new ExamplePupupContent ();

    void OnGUI ()
    {
        if (GUILayout.Button ("PopupContent",GUILayout.Width(128))) {
            var activatorRect = GUILayoutUtility.GetLastRect ();

            PopupWindow.Show (activatorRect, popupContent);
        }
    }
}

//弹窗
public class ExamplePupupContent : PopupWindowContent
{
    public override void OnGUI (Rect rect)
    {
        EditorGUILayout.LabelField ("Lebel");
    }

    public override void OnOpen ()
    {
        Debug.Log ("打开窗口");
    }

    public override void OnClose ()
    {
        Debug.Log ("关闭窗口");
    }

    public override Vector2 GetWindowSize ()
    {
        //Popup 弹窗的大小
        return new Vector2 (300, 200);
    }
}

效果如下:
这里写图片描述
这里写图片描述

窗口创建查找游戏物体

在Editor下创建脚本如下:

using UnityEditor;
using UnityEngine;

public class WindowExample2 : ScriptableWizard

{
    public string gameObjectName;
    [MenuItem("Window/WindowExample2")]
    static void Open ()
    {
        DisplayWizard<WindowExample2> ("TitleName", "Create", "Find");

    }
//  创建一个GameObject
    void OnWizardCreate ()
    {
        new GameObject (gameObjectName);
    }
    void OnWizardOtherButton ()
    {
        var gameObject = GameObject.Find (gameObjectName);

        if (gameObject == null)
        {
            Debug.Log ("没有找到相关物体");
        }
    }
    void OnWizardUpdate ()
    {
        Debug.Log ("Update");
    }
}

效果如下:
这里写图片描述

为窗口添加图标和菜单

在Editor下创建脚本如下:

using UnityEditor;
using UnityEngine;

public class WindowExample3 : EditorWindow,IHasCustomMenu
{
//  给窗口添加菜单
    public void AddItemsToMenu (GenericMenu menu)
    {
        menu.AddItem (new GUIContent ("example"), false, () => {

        });

        menu.AddItem (new GUIContent ("example2"), true, () => {
        });
    }



    [MenuItem ("Window/WindowExample3")]
    static void Open ()
    {
        var window = GetWindow<WindowExample3> ();

//      设置窗口最大和最小尺寸,这里最大和最小相等,相当于尺寸不可改变
        window.maxSize = window.minSize = new Vector2 (300, 300);
//      设置窗口图标
        var icon = AssetDatabase.LoadAssetAtPath<Texture> ("Assets/Editor/Mondeville.png");
//      设置窗口标题
        window.titleContent = new GUIContent ("Hoge", icon);

    }
}

效果如下:
这里写图片描述

本文工程:http://download.csdn.net/detail/warrenmondeville/9700712
本文链接:http://write.blog.csdn.net/mdeditor#!postId=53444808

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!