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

一、一个目标类-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绘制。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值