您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 2浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

三、Unity编辑器开发之CustomEditor

CoderZ1010 发布时间:2021-02-26 13:32:04 ,浏览量:2

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");
    }
}

具体如何绘制自定义检视面板,在后续文章中进行介绍。

关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.0512s