Unity_编辑器扩展入门使用

发布于 2020-05-08  13 次阅读


前言

EditorWindow和Inspector属性必不可少,之前也用过和了解过,这次系统的学一下。

正文

先说几个添加按钮的方法

  • MenuItem 在菜单栏加item
    [MenuItem("MyMenu/Do Something")]
  • AddComponentMenu 在组件菜单中加按钮
    [AddComponentMenu("Transform/Follow Transform")]
  • ContextMenu 给脚本添加右键命令
    [ContextMenu("Test Context")]
  • CreateAssetMenu 用于创建Asset
    [CreateAssetMenu(fileName = "x",menuName = "(custom)XX")]

EditorWindow窗口开发

实现效果:

实现思路:

  • 继承EditorWindow类
  • OnGUI方法
  • EditorGUILayout静态类。
using UnityEditor;
using UnityEngine;  
public class PanWindow : EditorWindow
{
    [MenuItem("Game Framework/Pan", false, 41)]
    private static void Open()
    {
        //到这里我们的窗口就实现了,接下来要填充窗口里的东西了
        Rect area = new Rect(0, 0, 600, 400);
        PanWindow window = GetWindowWithRect<PanWindow>(area, true, "Panpan窗口"); //限定大小 
        //PanWindow window = (PanWindow)EditorWindow.GetWindow(typeof(PanWindow)); //类似Inspector这种的
        window.Show(); 
    }
    public enum QEnum
    {
        None,
        One,
        Two
    }
    string myString = string.Empty;
    bool isBool = false;
    float myFloat = 0.0f;
    Color color;
    QEnum qEnum;
    GameObject go;
    private void OnGUI()
    {
        GUIStyle buttonStyle = new GUIStyle(); //声明一个样式
        buttonStyle.alignment = TextAnchor.MiddleCenter;
        buttonStyle.fontSize = 15;
        buttonStyle.fixedWidth = 15;
        buttonStyle.normal.textColor = Color.white;
        buttonStyle.normal.background = null;
        //默认是一行一行的往下走    
        EditorGUILayout.Space(); //空格
        //标签
        GUILayout.Label("Base Settings", EditorStyles.boldLabel);
        EditorGUILayout.BeginHorizontal();

        //GUILayout.Width(80) 定义自己的长度
        EditorGUILayout.LabelField("账号:", GUILayout.Width(80));  
        //myString = EditorGUILayout.TextField("账号:", myString, GUILayout.Width(100));//这样排版不太好

        myString = EditorGUILayout.TextField(string.Empty, myString, GUILayout.Width(100));
        if (GUILayout.Button("生成", GUILayout.Width(40)))
        {

        }
        if (GUILayout.Button("⊙", buttonStyle))
        {

        }
        EditorGUILayout.EndHorizontal(); //下一行

        //控制子节点勾选
        isBool = EditorGUILayout.BeginToggleGroup("Optional Setting", isBool);
        isBool = EditorGUILayout.Toggle("Toggle",isBool);
        myFloat = EditorGUILayout.Slider("Slider", myFloat,-3,3);
        EditorGUILayout.EndToggleGroup();

        //颜色
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("给你点颜色看看:", GUILayout.Width(160));
        color = EditorGUILayout.ColorField( color,   GUILayout.Width(100));
        EditorGUILayout.EndHorizontal(); //下一行

        //文本框
        EditorGUILayout.BeginVertical("box", GUILayout.Width(160), GUILayout.Height(100));
        myString = EditorGUILayout.TextArea("sss", myString, GUILayout.Width(160), GUILayout.Height(100));
        EditorGUILayout.EndVertical();
        //枚举
        qEnum = (QEnum)EditorGUILayout.EnumPopup("qEnum",qEnum, GUILayout.Width(220));

        go = EditorGUILayout.ObjectField(new GUIContent("物体:","说明??"), go,typeof(GameObject),true)as GameObject;

    }
}

Inspector属性开发

实现效果:

实现思路:

  • 继承Editor类
  • 重写 override OnInspectorGUI()

Player.cs

using System.Collections.Generic;
using UnityEngine; 
public enum ETYPE
{
    co,
    vi
}
public class Player : MonoBehaviour
{ 
    public List<UIStep> list_Step; 
    public UnityEngine.Events.UnityEvent OnTimeCountDown;
}

[System.Serializable]
public class UIStep { 
    //标题
    public string TitleStr;
    //对话内容
    public string DialogueStr;
    //播放的音频名称
    public string AudioNameStr; 
    public int CountTimeInt;
    //倒计时后发送的消息
    public string CountTimeMsgStr; 
    public int slide; 
    public ETYPE etype; 
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Player))]
public class PlayerScriptsObjectsEditor :Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI(); //这句话会显示父类的属性
        EditorGUILayout.Space();
        //这里是重写

        EditorGUILayout.LabelField("StepInfo");

        Player player = (Player)target;

        for (int i = 0; i < player.list_Step.Count; i++)
        { 
            GUI.color = Color.white;

            #region 步骤标题
            EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
            //步骤标题渲染
            GUI.color = Color.blue;
            EditorGUILayout.LabelField("Step" + i, GUILayout.MaxHeight(30), GUILayout.MaxWidth(50));
            GUI.color = Color.white;
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            //文档配置文件
            player.list_Step[i].TitleStr = EditorGUILayout.TextField("标题", player.list_Step[i].TitleStr);
            player.list_Step[i].DialogueStr = EditorGUILayout.TextField("对话窗", player.list_Step[i].DialogueStr);
            player.list_Step[i].AudioNameStr = EditorGUILayout.TextField("音乐名字", player.list_Step[i].AudioNameStr);
            player.list_Step[i].CountTimeInt = EditorGUILayout.IntField("倒计时", player.list_Step[i].CountTimeInt);
            player.list_Step[i].CountTimeMsgStr = EditorGUILayout.TextField("倒计时后消息", player.list_Step[i].CountTimeMsgStr);

            player.list_Step[i].slide = EditorGUILayout.IntSlider("as", player.list_Step[i].slide,0,100);

            EditorGUILayout.BeginHorizontal(EditorStyles.helpBox); 
            EditorGUILayout.LabelField("消息类型");
            player.list_Step[i].etype = (ETYPE)EditorGUILayout.EnumPopup(player.list_Step[i].etype);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
            EditorGUILayout.LabelField("该步骤可见的物体");
            GUI.color = Color.green;
            if (GUILayout.Button("添加", new GUIStyle(EditorStyles.miniButtonRight)))
            {
                Debug.Log(111);
            }
            GUI.color = Color.white;

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();

            #endregion

            EditorGUILayout.BeginVertical();
            #region 步骤插入组件
            EditorGUILayout.Space();
            GUI.color = Color.yellow;
            if (GUILayout.Button("插入步骤", new GUIStyle(EditorStyles.miniButton), GUILayout.MaxWidth(60), GUILayout.MaxHeight(20)))
            {

                EditorGUILayout.Space();
            }
            #endregion
            #region 步骤删除组件
            EditorGUILayout.Space();
            GUI.color = Color.red;
            if (GUILayout.Button("删除步骤", new GUIStyle(EditorStyles.miniButton), GUILayout.MaxWidth(60), GUILayout.MaxHeight(20)))
            { 
                player.list_Step.RemoveAt(i); 
                EditorGUILayout.Space();
            }
            #endregion
            EditorGUILayout.EndVertical(); 
            EditorGUILayout.EndHorizontal(); 
        } 
        GUI.color = Color.green;
        if (GUILayout.Button("添加步骤", "toolbarButton", GUILayout.MaxWidth(120), GUILayout.MinWidth(120)))
        {
            //增加步骤
            player.list_Step.Add(new UIStep()); 
        }

    }
}

不积跬步,无以至千里;不积小流,无以成江海。