unity编辑器功能扩展(2)

了解unity界面编辑的由来,自己创造新的编辑界面,是不是很有意思呢?今天给大家介绍较基本的编辑功能,想深入的可以去官方API学习UnityEditor;
例子主要以脚本形式给出,脚本上都有注释,编辑器脚本应放在Editor文件夹下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test02 : MonoBehaviour {
    [Header("AttributeOne")]
    [Tooltip("鼠标附上去出现的提示信息")]
    public float life = 0;
    [Range(0, 1)]
    public float xiaoshu = 0.5f;
    [Range(0, 10)]
    public int zhengshu = 5;
    [Header("AttributeTwo")]
    public float index = 0.2f;
    [Space(5)]
    public float index2 = 0.5f;
    [System.NonSerialized]
    public string Name = "likang";
    [SerializeField]
    [Space(5)]
    private string mingzi = "xiaoming";
    [System.Serializable] //仅对类、结构体、委托、枚举有效,对字段和属性不可以
    public class SerializableClass
    {
        public int a = 9;
        public Vector2 pos;
        public Color _color;
        public Sprite _sprite;
    }
    public SerializableClass A; //使类显示在inspector上
    public readonly int shu = 6;
    public const int shu2 = 8;  //readonly const static 三者即使序列化也不会显示在inspector上
    public static int shu3 = 6;
    public int num;
    [HideInInspector]
    public bool isAlive;
    [HideInInspector]
    public Color headcolor;
    //在inspector面板上,鼠标浮到test02组件上,右键弹出控制选项,ContextMenu("")就是添加控制选项
    //来达到对应的目的,在编辑器模式下运行
    [ContextMenu("Init3")]
    void Init()
    {
        life = 9;
        print(life);
    }
   public void randomNum(int num)
    {
        print(num);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//using System;
using UnityEditor.SceneManagement;

[CustomEditor(typeof(test02))]  //对脚本组件test02扩展编辑器功能,自身脚本放在Editor文件夹下
public class editorTest : Editor {

    test02 script;
    GameObject rootobject;
    SerializedObject seriobject;
    SerializedProperty headcolor;
    private static bool toggle = true;
    //初始化
    public void OnEnable()
    {
        seriobject = base.serializedObject;
        headcolor = seriobject.FindProperty("headcolor");
        var tscript = (test02)base.serializedObject.targetObject;
        if (tscript != null)
        {
            script = tscript;
            rootobject = script.gameObject;
        }
        else
        {
            Debug.Log("tscript is null");
        }
    }
    //清理
    public void OnDisable()
    {
        var tscript = (test02)base.serializedObject.targetObject;
        if (tscript == null)
        {
            Debug.Log("tscript==null");
        }
        else
        {
            Debug.Log("tscript != null");
        }
        seriobject = null;
        script = null;
        rootobject = null;
    }
    [MenuItem("CONTEXT/Transform/RandomPosition")] //随机更改游戏物体上的transform的值上
    static void ContextMenu_RandomPosition()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel|SelectionMode.OnlyUserModifiable);
        foreach (Transform item in transforms)
        {
            item.position = new Vector3(Random.Range(-5,9),Random.Range(-9,8),Random.Range(0,9));
            Debug.Log(item.position);
        }
    }
    //在boxcollider组件上右键既可以看到aaa功能键,扩展boxcollider功能
    [MenuItem("CONTEXT/BoxCollider/aaa")] 
    static void ContextMenu_aaa()
    {
        Debug.Log("aaa");
    }
    //对test02属性面板显示控制
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        seriobject.Update();
        script = target as test02;
        if (GUILayout.Button("randomNum"))
        {
            Undo.RecordObject(script,"revert random num");
            script.randomNum(script.num);
        }
        GUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("saveScene"))
            {
                EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
            }
            if (GUILayout.Button(toggle ? "untoggle" : "toggle"))
            {
                toggle = !toggle;
                if (toggle)
                {
                    Debug.Log("来了");
                }
                else
                {
                    Debug.LogError("这是错误的红框吗");
                }
            }
        }
        GUILayout.EndHorizontal();

        script.isAlive = EditorGUILayout.BeginToggleGroup("isAlive",script.isAlive);
        if (script.isAlive)
        {
            script.life = EditorGUILayout.Slider("life",script.life,0,100);
        }
        EditorGUILayout.EndToggleGroup();
        EditorGUILayout.PropertyField(headcolor);
        seriobject.ApplyModifiedProperties();

        EditorGUILayout.LabelField("life"+script.life,GUILayout.Width(200));
        Repaint();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class editorTestTwo : Editor
{
    //在主菜单栏添加一个菜单,判断是否勾选
    const string Menu_Checked = "AAA/MenuChecked";
    const string Key_CheckedTwo = "MenuChecked";
    [MenuItem(Menu_Checked)]
    static void MenuChecked()
    {
        bool flag = Menu.GetChecked(Menu_Checked);
        if (flag)
        {
            Debug.Log("key_checked to 0");
            PlayerPrefs.SetInt(Key_CheckedTwo, 0);
        }
        else
        {
            Debug.Log("key_checked to 1");
            PlayerPrefs.SetInt(Key_CheckedTwo, 1);
        }
        Menu.SetChecked(Menu_Checked, !flag);
    }

    //右键Assets文件夹,在其右键栏中添加新功能
    [MenuItem("Assets/GGG")]
    static void MenuAssets()
    {
        if (Selection.activeObject == null)
        {
            Debug.Log("null");
        }
        else
        {
            Debug.Log("name:" + Selection.activeObject.name);
        }
    }
    //快捷键在场景中添加胶囊体,给美术玩耍
    [MenuItem("GameObject/Likang _y",false)]
    static void MenuGameObject()
    {
      GameObject gg=  GameObject.CreatePrimitive(PrimitiveType.Capsule);
      gg.transform.position = new Vector3(Random.Range(0,15),2,0);
      Debug.Log("king");
    }
    //在GameObject目录下,在hierarchy右键会出现该功能键
    [MenuItem("GameObject/SetActive _q", false)]  //下划线+字母 该功能的对应快捷键
    static void MenuGameObjectSetActive()
    {
        if (Selection.activeGameObject != null)
        {
            Undo.RecordObject(Selection.activeGameObject, Selection.activeGameObject.name);
            Selection.activeGameObject.SetActive(!Selection.activeGameObject.activeSelf);
            Debug.Log(Selection.activeObject.name);
        }
        else
        {
            Debug.Log("selectionObject is null");
        }
    }                                    //快捷键的对应
    [MenuItem("GameObject/dabao #q")]   //%--Ctrl #--Shift &--Alt LEFT/RIGHT/UP/DOWM--上下左右  _a_b_v_g.....普通字母快捷键
    static void dabaoMethod()
    {
        EditorUtility.DisplayCancelableProgressBar("dabao","进度",1f); //进度条
    }
    [MenuItem("GameObject/quxiao &LEFT")]
    static void quxiaoMethod()
    {
        EditorUtility.ClearProgressBar(); //取消进度条
    }
    [MenuItem("BBB/DuiHuaKuang")]
    static void duihuakuang()
    {
        EditorUtility.OpenFilePanel("aa","D:/",".txt"); //打开指定文件格式的文件
    }

    //没有选中物体时,菜单栏为灰色(不可选中)。只有当选中物体并且其类型为指定类型时,菜单栏才能被选中
    [MenuItem("DDD/自定义菜单学习")] 
    static void selectedGameObject()
    {
        Debug.Log("select this gameobject");
    }
    [MenuItem("DDD/自定义菜单学习",true)]
    static bool menu_study()
    {
        Object selectedObject = Selection.activeObject;
        if (selectedObject != null && selectedObject.GetType() == typeof(GameObject))
        {
            Debug.Log("正确选中");
            return true;
        }
        return false;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class editorTestThree : EditorWindow
{
    [MenuItem("AAA/SetWindow")]
    static void setDialog()
    {
        EditorWindow.GetWindow(typeof(editorTestThree)); //创建新窗口
    }
    public Object go = null;
    string goName = "default";
    float life = 10;
    bool isChanged = true;
    bool toggleChanged;

    private void OnGUI()
    {
        GUILayout.Label("KKK",EditorStyles.boldLabel);
        go = EditorGUILayout.ObjectField(go,typeof(Object),true);
        if (GUILayout.Button("button01"))
        {
            if (go == null)
            {
                Debug.Log("go is null");
            }
            else
            {
                Debug.Log(go.name);
            }
        }
        goName = EditorGUILayout.TextField("编辑器输入框",goName);
        toggleChanged = EditorGUILayout.BeginToggleGroup("选择设置",toggleChanged); //勾选框组,勾选打开组,否则关闭组
        if (toggleChanged)
        {
            isChanged = EditorGUILayout.Toggle("isChanged",isChanged); //勾选框
            life = EditorGUILayout.Slider("life",life,0,50);  //滑动条
        }
        EditorGUILayout.EndToggleGroup();
    }
}

猜你喜欢

转载自blog.csdn.net/fenglele_fans/article/details/80288841