
1. 先有一个普通的 继承自 MonoBehaviour 的脚本.
2. 创建一个 Editor 文件夹, 写 关于 UnityEditor 的脚本 都要放在这个文件夹下,不然会编译出错.
具体的实现如下:
using UnityEngine;
using UnityEditor;
using System.Collections; [CustomEditor(typeof(TestBehaviour))] // 这里是表示,这个Editor是哪个脚本的界面
[CanEditMultipleObjects] // 可以一起编辑多个物体
public class TestBehaviourInspector : Editor { // 继承 Editor 类
// 这里是定义几个你要去控制的那个脚本里参数.
// moveType 是一个 enum 类型
SerializedProperty isPlaying, Count, moveType; // 初始化, 将刚声明的参数与那个脚本里面的属性相关联
void OnEnable() {
isPlaying = serializedObject.FindProperty("isPlaying");
Count = serializedObject.FindProperty("Count");
moveType = serializedObject.FindProperty("moveType");
} // Editor GUI 具体界面在这里写
public override void OnInspectorGUI() {
// 官方说要先这样
serializedObject.Update(); // 使用一个 垂直 布局
EditorGUILayout.BeginVertical(); // 如果是选择多个物体的情况下. 要先判断一下,这个属性上面的值是不是都相同,相同才可以一起改变
if (!isPlaying.hasMultipleDifferentValues) {
isPlaying.boolValue = EditorGUILayout.Toggle("Is Playing", isPlaying.boolValue);
} // 这是如果处理 enum 类型
if (!moveType.hasMultipleDifferentValues) {
moveType.enumValueIndex = (int)(TestBehaviour.MoveType)EditorGUILayout.EnumPopup("Move Type", (TestBehaviour.MoveType)moveType.enumValueIndex);
} if (!Count.hasMultipleDifferentValues) {
Count.intValue = EditorGUILayout.IntField("Count", Count.intValue); // 还可以使用 Button
if (GUILayout.Button("Print Count")) {
Debug.Log(Count.intValue);
}
} EditorGUILayout.EndVertical(); // 官方说 要在这里这样一下.应用所有的修改
serializedObject.ApplyModifiedProperties();
}
}