Unity3d编辑器扩展学习笔记

编辑器扩展

1.添加菜单栏:把特性应用于静态方法

参数1:菜单名的空格后面是定义快捷键(单符号得用"_"开头,组合键%=Ctrl,#=Shift,&=Alt)
参数2:通过定义一个验证方法来控制是否启用菜单栏(如:当有选择到物体时,启用,否则禁用)
参数3:菜单栏中显示的顺序(优先级),按这个值可以分组(大概相差10就分一组)
可以为已有菜单添加子菜单,如Assets/MyTools,将显示在Assets菜单栏内,同时也会在Projiect窗口的右键菜单内显示。
[MenuItem("MyTools/test1 _t", false, 12)]
[MenuItem("MyTools/test2 %t", false, 12)]

2.利用Selection类的静态方法,可以在编辑器中获取到当前所选择的对象

3.利用Undo类的静态方法,可以执行一些可以撤销的操作

Undo.DestroyObjectImmediate(Selection.activeGameObject);

4.为组件添加右键菜单栏

方式1:可以为自定义组件、内置组件添加
必须以“CONTEXT”开始,方法的参数MenuCommand mc会被自动赋值为该组件所挂载的游戏物体

[MenuItem("CONTEXT/PlayerController/菜单名")]
static void ResetSize(MenuCommand mc)
{
    PlayerController pc = mc.context as PlayerController;
    //接下来就可以修改组件对象pc的属性了
    pc.Size = 12;
}

方式2:只能为自定义组件添加(可以编辑代码的组件),直接在该组件的代码内添加。
ContextMenu和ContextMenuItem类在UnityEngine命名空间下,与UnityEditor无关
a.为组件添加右键菜单栏

[ContextMenu("Set Color")]
void SetColor()
{
    bodyColor = Color.red;
}

b.为组件的属性添加右键菜单栏

[ContextMenuItem("Menu name", "AddSize")]
public int Size = 0;

void AddSize()
{
    Size += 10;
}

5.自定义Inspector面板

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
// 为组件EnemyManager自定义Inspector面板显示,CanEditorMutipleObjects 表示可以同时编辑多个物体
[CustomEditor(typeof(EnemyManager)),CanEditMultipleObjects]
public class EnemyManagerInspector : Editor
{
    // 这里用简单的显示控制来让大家理解自定义编辑器的流程
    public static SerializedProperty enemyList, bossEnemyList,size;
    void OnEnable()
    {
        enemyList = serializedObject.FindProperty("EnemyList");
        bossEnemyList = serializedObject.FindProperty("BossEnemyList");
    }
    private bool isOpenDelete = false;
    private bool isOpenAdd = true;
    private bool isShow = false;
    public override void OnInspectorGUI()
    {
        //表示只做附加显示,可为内置组件附加新的Inspector面板属性
        base.DrawDefaultInspector();
        // 更新编辑器显示的序列化属性
        serializedObject.Update();
        //获取组件对象
        EnemyManager enemyManager = (EnemyManager)target;
        //显示属性,true表示显示所有包含的成员
        EditorGUILayout.PropertyField(enemyList,true);
        //开始新的一行
        EditorGUILayout.BeginHorizontal();
        isOpenDelete = EditorGUILayout.BeginToggleGroup("是否允许删除", isOpenDelete); //包含一组元素,整体控制他们是否可编辑
        if (GUILayout.Button("删除成员"))
        {
        }
        EditorGUILayout.EndToggleGroup();
        EditorGUILayout.EndHorizontal();
        GUILayout.Label("**********分割线*********");
        // 接受序列化赋值
        serializedObject.ApplyModifiedProperties();
    }
}

6.自定义窗口

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow {
    [MenuItem("Window/show mywindow")]
    static void ShowMyWindow()
    {
        MyWindow window= EditorWindow.GetWindow<MyWindow>();
        window.Show();
    }
    private string name="";
    void OnGUI()
    {
        GUILayout.Label("这是我的窗口");
        name = GUILayout.TextField(name);
        if (GUILayout.Button("创建"))
        {
            GameObject go = new GameObject(name);
            //纪录操作,便于撤销
            Undo.RegisterCreatedObjectUndo(go, "create gameobject");
        }
    }
}

7.自定义对话窗口

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EnemyChange : ScriptableWizard {
    [MenuItem("Tools/CreateWizard")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard<EnemyChange>("统一修改敌人","Change And Close","Change");
    }
    public int changeStartHealthValue = 10;
    public int changeSinkSpeedValue = 1;
     const string changeStartHealthValueKey = "EnemyChange.changeStartHealthValue";
     const string changeSinkSpeedValueKey = "EnemyChange.changeSinkSpeedValue";
    //当窗口被创建出来的时候调用的
    void OnEnable()
     {
         changeStartHealthValue = EditorPrefs.GetInt(changeStartHealthValueKey, changeStartHealthValue);
         changeSinkSpeedValue = EditorPrefs.GetInt(changeSinkSpeedValueKey, changeSinkSpeedValue);
    }
    //检测create按钮的点击
    void OnWizardCreate()
    {
        GameObject[] enemyPrefabs = Selection.gameObjects; //获得选择的物体对象
        //显示进度条
        EditorUtility.DisplayProgressBar("进度", "0/" + enemyPrefabs.Length + " 完成修改值", 0);
        int count = 0;
        foreach (GameObject go in enemyPrefabs)
        {
            CompleteProject.EnemyHealth hp = go.GetComponent<CompleteProject.EnemyHealth>();
            Undo.RecordObject(hp, "change health and speed");
            hp.startingHealth += changeStartHealthValue;
            hp.sinkSpeed += changeSinkSpeedValue;
            count++;
            EditorUtility.DisplayProgressBar("进度", count+"/" + enemyPrefabs.Length + " 完成修改值", (float)count/enemyPrefabs.Length);
        }
        EditorUtility.ClearProgressBar();
        //显示提示信息
        ShowNotification(new GUIContent(Selection.gameObjects.Length + "个游戏物体的值被修改了"));
    }
    //检测Other按钮的点击
    void OnWizardOtherButton()
    {
        OnWizardCreate();
    }
    //当前字段值修改的时候会被调用
    void OnWizardUpdate()
    {
        errorString = null;
        helpString = null;
        if (Selection.gameObjects.Length > 0)
        {
            helpString = "您当前选择了" + Selection.gameObjects.Length + "个敌人"; //头部提示信息
        }
        else
        {
            errorString = "请选择至少一个敌人"; //底部警告信息
        }
        //保存面板的修改
        EditorPrefs.SetInt(changeStartHealthValueKey, changeStartHealthValue);
        EditorPrefs.SetInt(changeSinkSpeedValueKey, changeSinkSpeedValue);
    }
    void OnSelectionChange()
    {
        OnWizardUpdate();
    }
}

8.其它

当脚本挂载到物体上时,自动为它添加指定组件,如:[RequireComponent(typeof(Rigidbody))]
添加进菜单Component中,[AddComponentMenu("companentPathAndName")]

posted on 2017-05-27 12:23  SuriFuture  阅读(503)  评论(0编辑  收藏  举报

导航