Unity编辑器之Inspector


Unity中允许用户对脚本的属性进行自定义编辑,被编辑的类需要用[Serializable]修饰,编辑器类继承Editor并重写OnInspectorGUI()方法,具体的排版在这个方法中绘制。
常用的基本方法:
//获取属性 serializedObject.FindProperty("age");
//显示属性,类型会自动获取 EditorGUILayout.PropertyField(ageProp, new GUIContent("age"));
//应用修改项 serializedObject.ApplyModifiedProperties();
//对象需要一直更新 serializedObject.Update();

下面放代码,将RoleController.cs挂在物体上,将EditorInspector.cs放在工程的Editor目录。

using UnityEngine;
using System;
using System.Collections;
[Serializable]
public class RoleController :MonoBehaviour
{
    public string roleName = "asadasd";
    public int age;
    public float range;
    public float jumpHight;
    public Texture2D pic;
    public string picName;
    public moveType ac;
    public bool isBoy;
}
public enum moveType
{
    jump,
    move,
    attack
}

Editor脚本

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(RoleController))]
[CanEditMultipleObjects]
public class EditorInspector : Editor
{
    private RoleController role;

    bool toggle;
    SerializedProperty ageProp;
    SerializedProperty nameProp;
    SerializedProperty picProp;
    SerializedProperty rangeProp;
    SerializedProperty boolProp;
    SerializedProperty enumProp;
    void OnEnable()
    {
        // Setup the SerializedProperties.
        ageProp = serializedObject.FindProperty("age");
        nameProp = serializedObject.FindProperty("roleName");
        picProp = serializedObject.FindProperty("pic");
        rangeProp = serializedObject.FindProperty("range");
        boolProp = serializedObject.FindProperty("isBoy");
        enumProp = serializedObject.FindProperty("ac");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        role = target as RoleController;
        GUILayout.Space(6f);

        //role.age = EditorGUILayout.IntSlider("Age", role.age, 0, 100);
        //role.roleName = EditorGUILayout.TextField("角色名字", role.roleName);

        EditorGUILayout.PropertyField(ageProp, new GUIContent("age"));
        EditorGUILayout.PropertyField(nameProp, new GUIContent("name"));
        EditorGUILayout.PropertyField(boolProp, new GUIContent("isBoy"));
        EditorGUILayout.PropertyField(enumProp, new GUIContent("enum"));

        if (EditorGUILayout.Foldout(toggle, "折叠"))
        {
            EditorGUILayout.PropertyField(picProp, new GUIContent("pic"));
        }
        EditorGUILayout.Slider(rangeProp, 0, 100, new GUIContent("range"));
        ProgressBar(rangeProp.floatValue/100, "range");
        serializedObject.ApplyModifiedProperties();
    }

    // Custom GUILayout progress bar.
    void ProgressBar(float value, string label)
    {
        // Get a rect for the progress bar using the same margins as a textfield:
        Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
        EditorGUI.ProgressBar(rect, value, label);
        EditorGUILayout.Space();
    }
}

现在大家可以根据自己的需求进行拓展了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,796评论 19 139
  • 转自http://www.cnblogs.com/donghua/p/4957415.html Oculus/Ge...
    Moment__格调阅读 8,684评论 0 3
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,165评论 18 399
  • This article is a record of my journey to learn Game Deve...
    蔡子聪阅读 9,357评论 0 9
  • 如果我是一条小鱼, 我要游向蓝蓝大海, 为大海增一份生机。 如果我是一只喜鹊, 我要飞向茫茫田地, 为农民报一份喜...
    淡墨鲤阅读 1,085评论 0 2