Unity 编辑器扩展总结 三:自定义Inspector面板

编辑器扩展总结

工欲善其事必先利其器

引言: 在项目开发中,编辑器扩展为开发者提供了开发自定义工具的功能,让开发者更加便利地使用编辑器开发项目。如若博客中存在错误,还请不吝赐教。所有参考的博客或者视频来源将在文末展示。
开发版本: Unity 2018.1.3f1

相关博客传送门
一、编辑器开发入门

二、编辑器的相关特性

三、自定义Inspector面板

四、创建编辑器窗体

五、Gizmos辅助调试工具

六、扩展Scene视图

七、数组或list集合的显示方式

八、EditorPrefs、ScriptableObject、Undo

九、GUIStyle、GUISkin

十、AssetPostprocessor资源导入管线

自定义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();

参考资料

水鸡游戏课堂

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity编辑器扩展是指通过自定义脚本来扩展Unity编辑器的功能和界面。在Unity中,可以使用编辑器扩展来创建自定义面板、工具栏、菜单项等,以便更好地支持开发者的工作流程和需求。 在编辑器扩展中,有几个重要的类需要了解。首先是Editor类,它是所有自定义编辑器的基类,通过继承Editor类可以创建自定义Inspector面板,用于编辑特定组件的属性。其次是EditorWindow类,它是创建自定义窗口的基类,可以用于创建独立的编辑器工具窗口。然后是EditorGUI和EditorGUILayout类,它们用于在自定义面板中创建UI元素,如按钮、文本框、滑动条等。这两个类的区别在于EditorGUI需要手动计算UI元素的位置和大小,而EditorGUILayout会自动进行排版和计算坐标。此外,还有一些其他常用的类,如GUIGUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等,它们提供了各种用于编辑器扩展的功能和工具。 使用编辑器扩展可以提高开发效率,使开发者能够更方便地进行编辑器自定义和工作流程优化。通过自定义面板和工具,可以根据项目需求添加额外的功能和工具,提供更好的用户体验和开发环境。同时,编辑器扩展也为开发者提供了更多的自由度和创造力,可以根据自己的需求进行定制和扩展总结起来,Unity编辑器扩展是通过自定义脚本来扩展Unity编辑器的功能和界面,可以创建自定义面板、工具栏、菜单项等,提供更好的开发环境和用户体验。常用的类包括Editor、EditorWindow、EditorGUI、EditorGUILayout、GUIGUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值