当前位置:   article > 正文

Unity获取脚本的CustomEditor(自定义编辑)数据_unity 同个脚本多个customeditor

unity 同个脚本多个customeditor

在此之前,粗略的介绍下 CustomEditor(自定义编辑)。

Unity对于我们创建的Mono脚本提供了属性面板的展示和修改。默认情况下,Inspector面板中会显示当前脚本类的公开字段(public field),这些字段会被序列化并储存在挂载的预制上。而有些情况下我们需要自定义展示和修改面板数据展示,这时候就用到了Unity提供的CustomEditor。

CustomEditor特性:允许我们自定义当前脚本组件的Inspector检视面板。

使用方法:

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

参数介绍:

inspectedType:指定自定义那个类型的面板数据

editorForChildClasses :是否子类使用同样的自定义面板,默认不使用

使用示例片段:

  1. //TestData.cs
  2. using UnityEngine;
  3. /// <summary>
  4. /// 测试代码
  5. /// </summary>
  6. public class TestData : MonoBehaviour
  7. {
  8. [Range(0,1)]
  9. public float speed;
  10. }
  11. //TestDataEditor.cs
  12. using UnityEditor;
  13. /// <summary>
  14. /// 测试代码,自定义面板数据显示
  15. /// </summary>
  16. [CustomEditor(typeof(TestData))]
  17. public class TestDataEditor : Editor
  18. {
  19. private SerializedProperty _speed;
  20. TestData _target;
  21. private void OnEnable()
  22. {
  23. ///方式一
  24. _speed = serializedObject.FindProperty("speed");
  25. ///方式二
  26. //_target = (TestData)target;
  27. }
  28. public override void OnInspectorGUI()
  29. {
  30. //将_speed数据修改加入监听
  31. EditorGUI.BeginChangeCheck();
  32. /// 方式一
  33. serializedObject.Update();
  34. EditorGUILayout.PropertyField(_speed);
  35. serializedObject.ApplyModifiedProperties();
  36. ///方式二
  37. //_target.speed = EditorGUILayout.FloatField("速度", _target.speed);
  38. //_speed数据修改结束监听,并在发生修改时,做“脏”标记
  39. if (EditorGUI.EndChangeCheck())
  40. {
  41. EditorUtility.SetDirty(target);
  42. }
  43. }
  44. }

在开发过程中,为了给其他同学提供一些便利,通过自定义显示面板数据是很常见的一件事。但这会导致我们有时直接获取的当前组件数据时,发现不是面板上序列化储存的值。出现这种原因是因为Editor这边直接执行了ApplyModifiedProperties,这时候我们可以通过序列化对象获取想要的数据。

  1. using UnityEngine;
  2. using UnityEditor;
  3. /// <summary>
  4. /// 测试代码
  5. /// </summary>
  6. public class TestData : MonoBehaviour
  7. {
  8. [Range(0,1)]
  9. public float speed;
  10. /// <summary>
  11. /// 根据使用场景调用方法
  12. /// </summary>
  13. private void TestFunc()
  14. {
  15. //序列化当前对象
  16. SerializedObject obj = new SerializedObject(this);
  17. //查找想要的数据
  18. var _speed = obj.FindProperty("speed");
  19. //进行日志输出或GUI绘制
  20. Debug.LogError($"原始数据:{speed},序列化后的数据:{_speed}");
  21. }
  22. }

总结:我们要想在编辑器这边获取CustomEditor(自定义编辑)类的序列化数据时,只需要将当前类对象序列化即可。

资料查找:

1).Unity - Scripting API: CustomEditorUnity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.https://docs.unity3d.com/530/Documentation/ScriptReference/CustomEditor.html

2).Unity - Scripting API: CustomEditor.CustomEditorUnity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.https://docs.unity3d.com/530/Documentation/ScriptReference/CustomEditor-ctor.html

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

闽ICP备14008679号