Unity自定义Editor

作者: IongX | 来源:发表于2017-05-31 17:19 被阅读115次

实现根据不同类型,在检视面板中展示不同的参数。

实体类:

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


public class CustomTest :MonoBehaviour {

    public enum TestMode
    {
        first,
        second,
        third
    }

    public TestMode myMode;

    public GameObject ObjectA;
    public GameObject ObjectB;
    public GameObject ObjectC;

}

自定义编辑器类:

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

[CustomEditor(typeof(CustomTest))]
public class CustomTestEditor : Editor {

    public override void OnInspectorGUI()
    {
        CustomTest test = (CustomTest) target;
        test.myMode = (CustomTest.TestMode) EditorGUILayout.EnumPopup("MyMode", test.myMode);
        EditorGUILayout.Space();

        switch (test.myMode)
        {
            case CustomTest.TestMode.first:
                test.ObjectA = (GameObject) EditorGUILayout.ObjectField("A",test.ObjectA,typeof(GameObject),true);
                break;
            case CustomTest.TestMode.second:
                test.ObjectB = (GameObject)EditorGUILayout.ObjectField("B", test.ObjectB, typeof(GameObject), true);
                break;
            case CustomTest.TestMode.third:
                test.ObjectC = (GameObject)EditorGUILayout.ObjectField("C", test.ObjectC, typeof(GameObject), true);
                break;
            default:
                break;
        }
    }
}

效果如下:

Paste_Image.png Paste_Image.png

相关文章

网友评论

    本文标题:Unity自定义Editor

    本文链接:https://www.haomeiwen.com/subject/jpsjfxtx.html