代码:重写 Inspector 面板属性自定义绘制, 自定义是否显示属性
using System;
using UnityEditor;
using UnityEngine;
[Serializable]
public class Args
{
public int age;
public GameObject player;
}
public class Test : MonoBehaviour
{
public bool propertySwitch;
public float names;
public Args ages;
}
[CustomEditor(typeof(Test))] //表名该类是某一个类的编辑器类
[CanEditMultipleObjects] //使该编辑器支持多对象
public class CusttomCameraControllerInpector : Editor
{
SerializedProperty _propertySwitch;
SerializedProperty _names;
SerializedProperty _ages;
SerializedProperty _players;
public override void OnInspectorGUI()
{
_propertySwitch = serializedObject.FindProperty("propertySwitch");
_names = serializedObject.FindProperty("names");
_ages = serializedObject.FindProperty("ages").FindPropertyRelative("age");
_players = serializedObject.FindProperty("ages").FindPropertyRelative("player");
serializedObject.Update();
EditorGUILayout.PropertyField(_propertySwitch, new GUIContent("属性开关"));
if (_propertySwitch.boolValue)
{
EditorGUILayout.PropertyField(_names, new GUIContent("名称"));
EditorGUILayout.PropertyField(_ages, new GUIContent("年龄"));
EditorGUILayout.PropertyField(_players, new GUIContent("对象"));
}
serializedObject.ApplyModifiedProperties();
}
}