Unity编辑器开发——自定义Inspector面板(重写OnInspectorGUI()方法)

卢黎明
2023-12-01

一、一个目标类-target

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

public enum Course
{
    Chinese,
    Mathematics,
    English
}

public class MyInspector : MonoBehaviour
{
    public int intValue;
    public float floatValue;
    public string stringValue;
    public bool boolValue;
    public Vector3 vector3Value;
    public Course enumValue = Course.Chinese;
    public Color colorValue = Color.white;
    public Texture textureValue;
}
 

二、一个编辑器类-Editor

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MyInspector))] //将要重绘的target类传给Editor类
public class TestInspector : Editor
{
    #region 法一
    /*//强转为目标类
    private MyInspector _target { get { return target as MyInspector; } }

    //InspectorGUI重绘
    public override void OnInspectorGUI()
    {
        EditorGUILayout.BeginVertical();

        _target.intValue = EditorGUILayout.IntField("ontValue", _target.intValue);                  //int --> IntField
        _target.floatValue = EditorGUILayout.FloatField("floatValue", _target.floatValue);          //float --> FloatField
        _target.stringValue = EditorGUILayout.TextField("stringValue", _target.stringValue);        //string --> TextField
        _target.boolValue = EditorGUILayout.Toggle("boolValue", _target.boolValue);                 //bool --> Toggle
        _target.vector3Value = EditorGUILayout.Vector3Field("vector3Value", _target.vector3Value);  //vector3 --> Vector3Field
        _target.enumValue = (Course)EditorGUILayout.EnumPopup("enumValue", (Course)_target.enumValue); //enum --> EnumPopup 需要强转
        _target.colorValue = EditorGUILayout.ColorField(new GUIContent("colorValue"), _target.colorValue);//color --> ColorField
        _target.textureValue = (Texture)EditorGUILayout.ObjectField("textureValue", _target.textureValue, typeof(Texture), true); //texture -->

        EditorGUILayout.EndVertical();
    }*/ 
    #endregion

    #region 法二  此脚本效果和Base.OnInspectorGUI即默认效果一样
    /*//自定义序列化属性
    private SerializedProperty intValue;
    private SerializedProperty floatValue;
    private SerializedProperty stringValue;
    private SerializedProperty boolValue;
    private SerializedProperty vector3Value;
    private SerializedProperty enumValue;
    private SerializedProperty colorValue;
    private SerializedProperty textureValue;

    private void OnEnable()
    {
        //通过名字查找序列化属性
        intValue = serializedObject.FindProperty("intValue");
        floatValue = serializedObject.FindProperty("floatValue");
        stringValue = serializedObject.FindProperty("stringValue");
        boolValue = serializedObject.FindProperty("boolValue");
        vector3Value = serializedObject.FindProperty("vector3Value");
        enumValue = serializedObject.FindProperty("enumValue");
        colorValue = serializedObject.FindProperty("colorValue");
        textureValue = serializedObject.FindProperty("textureValue");
    }

    public override void OnInspectorGUI()
    {
        //表示更新序列化物体
        serializedObject.Update();
        EditorGUILayout.PropertyField(intValue);
        EditorGUILayout.PropertyField(floatValue);
        EditorGUILayout.PropertyField(stringValue);
        EditorGUILayout.PropertyField(boolValue);
        EditorGUILayout.PropertyField(vector3Value);
        EditorGUILayout.PropertyField(enumValue);
        EditorGUILayout.PropertyField(colorValue);
        EditorGUILayout.PropertyField(textureValue);
        //应用修改的属性值,不加的话,Inspector面板修改不了
        serializedObject.ApplyModifiedProperties();
    }*/
    #endregion
}
 

PS:第二种绘制方式相较于第一种,显示的效果是差不多的。虽然脚本内容多了一点,但是方式比较简单。不用根据每个变量的数据类型选择相对应的属性API绘制。

 类似资料: