当前位置:   article > 正文

三、Unity编辑器开发之CustomEditor_unity customeditor

unity customeditor

CustomEditor特性,允许我们自定义组件的Inspect检视面板。

  1. public CustomEditor (Type inspectedType);
  2. public CustomEditor (Type inspectedType, bool editorForChildClasses);

param1: inspectedType 检视的类型,即自定义哪个类型的Inspector。

param2: editorForChildClasses 默认为false,为true时表明其子类使用同样的Inspector。

例如,我们创建一个Person组件:

  1. using UnityEngine;
  2. public class Person : MonoBehaviour
  3. {
  4. public string Name;
  5. public int Age;
  6. public float Weight;
  7. }

检视面板显示了Person组件中的三个公开字段:

接下来自定义该组件的检视面板,首先需要在Editor文件夹中创建一个PersonInspector.cs脚本

引入命名空间UnityEditor后,为该类添加CustomEditor特性,并继承Editor类:

  1. using UnityEditor;
  2. [CustomEditor(typeof(Person))]
  3. public class PersonInspector : Editor {}

接下来重写OnInspectorGUI方法来自定义我们所需要的内容:

比如在面板上显示一个字符串:

  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(Person))]
  4. public class PersonInspector : Editor
  5. {
  6. public override void OnInspectorGUI()
  7. {
  8. base.OnInspectorGUI();
  9. GUILayout.Label("Editor Extension...");
  10. }
  11. }

再比如在面板上添加一个按钮:

  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(Person))]
  4. public class PersonInspector : Editor
  5. {
  6. public override void OnInspectorGUI()
  7. {
  8. base.OnInspectorGUI();
  9. GUILayout.Label("Editor Extension...");
  10. GUILayout.Button("Button");
  11. }
  12. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/806558
推荐阅读
相关标签
  

闽ICP备14008679号