Unity游戏编程定制编辑器(一)

来源:互联网 发布:灵云手写输入软件 编辑:程序博客网 时间:2024/05/17 03:37

1 扩展菜单栏

using UnityEngine;using UnityEditor;public class TestEditorAbout{    // 添加菜单    [MenuItem("Test/About Test Editor")]    static private void DisplayAbout()    {                EditorUtility.DisplayDialog("About", "Test Editor. \n\nBy Ldlan 2015-9-24 15:37", "OK");    }}
添加主菜单Test,并且在Test下添加子菜单:About Test Editor

点击子菜单将会调用DisplayAbout函数,上例中将会弹出对话框。


2 窗口编辑窗口

编辑窗口的显示,一般的窗口弹出在菜单中进行,即点击菜单弹出窗口,或者也可以在Inspector中提供弹出窗口的按钮,在Inspector中添加按钮涉及到Inspector的扩展,其一般是在自定义的组件的基础上进行的。

在菜单中弹出窗口

public class TestOpenEditor{    [MenuItem("Test/Open Editor %t")]    static void OpenEditor()    {        TestEditor.GetWindow<TestEditor>();    }}


其它一些快捷键控制

% – CTRL on Windows / CMD on OSX

# – Shift

& – Alt

LEFT/RIGHT/UP/DOWN – Arrow keys

F1…F2 – F keys

HOME, END, PGUP, PGDN


扩展窗口的方式如下,需要继承EditorWindow

public class TestEditor: EditorWindow{    static void Init()    {        // Init code    }}
Init为一般的初始化函数,一般将添加菜单与初始化结合一起放在TestEditor中

public class TestEditor: EditorWindow{    [MenuItem("Test/Test Editor %t")]    static void Init()    {        TestEditor.GetWindow<TestEditor>();        // Init code    }}

完整的编辑窗口代码

public class TestEditor: EditorWindow{    [MenuItem("Test/Test Editor %t")]    static void Init()    {        TestEditor.GetWindow<TestEditor>();    }    void OnEnable()    {        // Init UI        minSize = new Vector2(800, 400);    }    void OnDisable()    {        // Destory & Release    }    void OnInspectorUpdate()    {        // Repaint        Repaint();    }    void OnGUI()    {        // Draw UI    }}

一些初始化或清除工作

OnEnable函数:一些UI的初始化工作,例如窗口的最小长宽的指定

OnDisable函数:UI或资源的一些清除释放工作,也可以在OnDestroy中进行

UI等更新

OnInspectorUpdate:Inspector更新函数,在其内调用了Repaint保证编辑窗口的实时更新,尤其对动态控件或数据需要实时更新显示时,如果没有发生更新这是没有Repaint的原因

OnGUI:窗口的更新,自定义编辑窗口的绘制放在这里

其他的更新事件,比如如果你的自定义编辑窗口中涉及一些动画的播放时,需要另外的动画更新函数,可以这样实现:

void OnEnable(){    minSize = new Vector2(800, 400);    mPreviousTime = EditorApplication.timeSinceStartup;    EditorApplication.update += AnimatorSceneUpdate;}void OnDisable(){    EditorApplication.update -= AnimatorSceneUpdate;        if(objAnimator)         GameObject.DestroyImmediate(objAnimator);}

窗口的绘制


完整的代码

using UnityEngine;using UnityEditor;public class TestEditor: EditorWindow{    [MenuItem("Test/Test Editor %t")]    static void Init()    {        TestEditor.GetWindow<TestEditor>();    }    void OnEnable()    {        // Init UI        minSize = new Vector2(800, 200);    }    void OnDisable()    {        // Destory & Release    }    void OnInspectorUpdate()    {        // Repaint        Repaint();    }    // EditorGUILayout    private bool changed = false;    void OnGUI()    {                GUILayout.BeginHorizontal();        {            //EditorGUI.BeginChangeCheck();                        DrawTargetCheck();            DrawTargetView();            DrawTargetDescription();            //EditorGUI.EndChangeCheck();        }        GUILayout.EndHorizontal();        GUILayout.BeginHorizontal();        {            DrawTools();        }        GUILayout.EndHorizontal();    }    Vector2 scrollTarget;    private int targetSelected=0;    string[] targets = { "Object Select", "Built-in Select", "Toggle Select", "Slider Select", "None Select"};    private void DrawTargetCheck()    {        GUILayout.BeginVertical(GUILayout.Width(200));        {            GUILayout.Label("Target");            GUILayout.BeginVertical("Window");            {                scrollTarget = GUILayout.BeginScrollView(scrollTarget);                targetSelected = GUILayout.SelectionGrid(targetSelected, targets, 1);                GUILayout.EndScrollView();            }                       GUILayout.EndVertical();        }        GUILayout.EndVertical();    }    Vector2 srollTargetView;    UnityEngine.Object testObject;    int testInt;    float testFloat;    bool testBool;    float testSliderValue;    private void DrawTargetView()    {        GUILayout.BeginVertical(GUILayout.Width(300));        {            GUILayout.Label("Target View");            GUILayout.BeginVertical("Box");            {                srollTargetView = GUILayout.BeginScrollView(srollTargetView);                {                    if (targetSelected < 0 || targetSelected > 3)                        ShowNotification(new GUIContent("Select a MecanimEventData object first."));                    else                    {                        RemoveNotification();                        switch (targetSelected)                        {                            case 0:                                {                                    testObject = EditorGUILayout.ObjectField("Object Type", testObject, typeof(UnityEngine.Object), false);                                }                                break;                            case 1:                                {                                    testInt = EditorGUILayout.IntField("Int Type", testInt);                                    testFloat = EditorGUILayout.FloatField("Float Type", testFloat);                                }                                break;                            case 2:                                {                                    testBool = EditorGUILayout.Toggle("Toggle", testBool);                                }                                break;                            case 3:                                {                                    testSliderValue = EditorGUILayout.Slider("Slider", testSliderValue, 0f, 1f);                                }                                break;                        }                    }                }                GUILayout.EndScrollView();            }            GUILayout.EndVertical();        }        GUILayout.EndVertical();    }    Vector2 srollTargetDescription;    private void DrawTargetDescription()    {        GUILayout.BeginVertical();        {            GUILayout.Label("Target Description");            GUILayout.BeginVertical("Box");            {                srollTargetDescription = GUILayout.BeginScrollView(srollTargetDescription);                {                    switch (targetSelected)                    {                        case 0:                            {                                GUILayout.Label("Base class for all objects Unity can reference.\n" +                                                "Any public variable you make that derives from Object \n" +                                                "gets shown in the inspector as a drop target, allowing \n" +                                                 "you to set the value from the GUI.");                            }                            break;                        case 1:                            {                                GUILayout.Label("Make a text field for entering Built-in type.");                            }                            break;                        case 2:                            {                                GUILayout.Label("Make a toggle.");                            }                            break;                        case 3:                            {                                GUILayout.Label("Make a slider the user can drag to change a value \n"+ "between a min and a max.");                            }                            break;                    }                }                GUILayout.EndScrollView();            }            GUILayout.EndVertical();        }        GUILayout.EndVertical();    }    private void DrawTools()    {        GUILayout.BeginHorizontal();        GUILayout.FlexibleSpace();        if (GUILayout.Button("View", GUILayout.Width(80), GUILayout.Height(30)))        {        }        EditorGUI.BeginDisabledGroup(!changed);           if (GUILayout.Button("Save", GUILayout.Width(80), GUILayout.Height(30)))        {        }        EditorGUI.EndDisabledGroup();        GUILayout.EndHorizontal();    }}public class TestEditorAbout : EditorWindow{    // 添加菜单    [MenuItem("Test/About Test Editor")]    static private void DisplayAbout()    {        TestEditorAbout.GetWindow<TestEditorAbout>();    }    void OnEnable()    {        minSize = new Vector2(400, 125);    }    void OnGUI()    {        GUILayout.Label("About\n\nTest Editor. \n\n\nBy Ldlan 2015-9-24");        GUILayout.BeginHorizontal();        GUILayout.FlexibleSpace();        if (GUILayout.Button("确定", GUILayout.Width(100), GUILayout.Height(40)))        {            Close();        }        GUILayout.EndHorizontal();    }}


0 0
原创粉丝点击