赞
踩
CustomEditor特性,允许我们自定义组件的Inspect检视面板。
- public CustomEditor (Type inspectedType);
- public CustomEditor (Type inspectedType, bool editorForChildClasses);
param1: inspectedType 检视的类型,即自定义哪个类型的Inspector。
param2: editorForChildClasses 默认为false,为true时表明其子类使用同样的Inspector。
例如,我们创建一个Person组件:
- using UnityEngine;
-
- public class Person : MonoBehaviour
- {
- public string Name;
- public int Age;
- public float Weight;
- }
检视面板显示了Person组件中的三个公开字段:
接下来自定义该组件的检视面板,首先需要在Editor文件夹中创建一个PersonInspector.cs脚本
引入命名空间UnityEditor后,为该类添加CustomEditor特性,并继承Editor类:
- using UnityEditor;
-
- [CustomEditor(typeof(Person))]
- public class PersonInspector : Editor {}
接下来重写OnInspectorGUI方法来自定义我们所需要的内容:
比如在面板上显示一个字符串:
- using UnityEngine;
- using UnityEditor;
-
- [CustomEditor(typeof(Person))]
- public class PersonInspector : Editor
- {
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- GUILayout.Label("Editor Extension...");
- }
- }
再比如在面板上添加一个按钮:
- using UnityEngine;
- using UnityEditor;
-
- [CustomEditor(typeof(Person))]
- public class PersonInspector : Editor
- {
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- GUILayout.Label("Editor Extension...");
- GUILayout.Button("Button");
- }
- }
具体如何绘制自定义检视面板,在后续文章中进行介绍。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。