编辑器扩展总结
工欲善其事必先利其器
引言: 在项目开发中,编辑器扩展为开发者提供了开发自定义工具的功能,让开发者更加便利地使用编辑器开发项目。近期小生一直在学习编辑器扩展的知识,发现网络上关于编辑器知识点的博客较为零散且混乱。当然,有一些大佬已经总结的很好了,小生这就算是狗尾续貂,主要目的为自我学习,近期会整理一系列编辑器相关的博客,分享给每一位在学习道路上奋斗的童鞋。如若博客中存在错误,还请大佬们不吝赐教。所有参考的博客或者视频来源将在文末展示。
开发版本: Unity 2018.1.3f1
相关博客传送门
一、编辑器开发入门
自定义Inspector面板
实现自定义Inspector面板的功能,主要有两种方式,可以看一下B站的水鸡大佬的【水鸡游戏课堂】(大佬的讲课速度有点偏快,背景音乐有点嘈杂,初学者有点吃力【小声说^ _ ^】)
创建目标类,挂载在场景对象中
using UnityEngine;
public enum Course
{
Chinese,
Mathematics,
English
}
public class InspectorExample : 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;
}
绘制方式一
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(InspectorExample))]
public class InspectorExampleEditor : Editor
{
//target指该编辑器类绘制的目标类,需要将它强转为目标类
private InspectorExample _target { get { return target as InspectorExample; } }
//GUI重新绘制
public override void OnInspectorGUI()
{
//EditorGUILayout.LabelField("IntValue",_target.intValue.ToString(),EditorStyles.boldLabel);
//_target.intValue = EditorGUILayout.IntSlider(new GUIContent("Slider"),_target.intValue, 0, 10);
//_target.floatValue = EditorGUILayout.Slider(new GUIContent("FloatValue"), _target.floatValue, 0, 10);
_target.intValue = EditorGUILayout.IntField("IntValue", _target.intValue);
_target.floatValue = EditorGUILayout.FloatField("FloatValue", _target.floatValue);
_target.stringValue = EditorGUILayout.TextField("StringValue", _target.stringValue);
_target.boolValue = EditorGUILayout.Toggle("BoolValue", _target.boolValue);
_target.vector3Value = EditorGUILayout.Vector3Field("Vector3Value", _target.vector3Value);
_target.enumValue = (Course)EditorGUILayout.EnumPopup("EnumValue", (Course)_target.enumValue);
_target.colorValue = EditorGUILayout.ColorField(new GUIContent("ColorValue"), _target.colorValue);
_target.textureValue = (Texture)EditorGUILayout.ObjectField("TextureValue", _target.textureValue, typeof(Texture), true);
}
}
绘制方式二
using UnityEditor;
[CustomEditor(typeof(InspectorExample))]
public class InspectorExampleEditor : Editor
{
//定义序列化属性
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();
}
}
P.S. 第二种绘制方式相较于第一种,显示的效果是差不多的。虽然脚本内容多了一点,但是方式比较简单。不用根据每个变量的数据类型选择相对应的属性API绘制。
布局
//水平和垂直布局,注意这是一个方法对,Begin和End不能少
EditorGUILayout.BeginVertical("box");
//TODO
EditorGUILayout.EndVertical();
EditorGUILayout.BeginHorizontal("box");
//TODO
EditorGUILayout.EndHorizontal();