自定义Inspector面板

 

 

using UnityEditor;
using UnityEngine;

public enum MyType {
    Dev,
    Master
}

public class TestCode : MonoBehaviour {
    public string testStr;
    public bool isTest;
    public MyType myType;
    public float testNum;
}

[CustomEditor(typeof(TestCode))]
public class TestCodeHelp : Editor 
{
    public override void OnInspectorGUI() {
        serializedObject.Update();
        
        //此处映射上面的字段
        EditorGUILayout.PropertyField(serializedObject.FindProperty("testStr"), new GUIContent("测试字符串"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("isTest"), new GUIContent("是否测试"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("myType"), new GUIContent("类型"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("testNum"), new GUIContent("数字"));

        //绘制按钮
        if (GUILayout.Button("Step1: Play Scene"))
        {
            Debug.Log("test === ");
        }
        
        //让面板上面的值可以修改
        serializedObject.ApplyModifiedProperties();
    }
}

 

使用 override 的话,面板里面的都会被重置掉,如果只是补充,可以通过调用 base.OnInspectorGUI() 来保留本身的样式,后面再添加自定义的样式即可
posted @ 2023-03-06 11:44  三页菌  阅读(34)  评论(0编辑  收藏  举报