Serialized

作者:追风剑情 发布于:2015-11-14 20:23 分类:Unity3d

序列化对象 与 自定义Inspector属性面板

一、创建MyTransform.cs、TransformInspector.cs


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MyTransform : MonoBehaviour {
  5.  
  6. public MySerializable mySerializable;
  7.  
  8. [SerializeField]
  9. private float mscale;
  10.  
  11. void OnEnable()
  12. {
  13. Debug.Log ("mscale="+mscale);
  14. Debug.Log ("mySerializable.namber="+mySerializable.namber);
  15. Debug.Log ("mySerializable.name="+mySerializable.name);
  16. }
  17.  
  18. }
  19.  
  20. [System.Serializable]
  21. public class MySerializable
  22. {
  23. public int namber;
  24. public string name;
  25. }
  1. //自定义MyTransform组件的检视面板
  2. using UnityEngine;
  3. using UnityEditor;
  4.  
  5. [CanEditMultipleObjects]
  6. [CustomEditor(typeof(MyTransform))]
  7. public class TransformInspector : Editor {
  8.  
  9. SerializedProperty m_scale;
  10. SerializedProperty m_serializable;
  11. SerializedProperty m_serializable_number;
  12. SerializedProperty m_serializable_name;
  13. void OnEnable () {
  14. m_scale = serializedObject.FindProperty ("mscale");
  15. m_serializable = serializedObject.FindProperty ("mySerializable");
  16. m_serializable_number = serializedObject.FindProperty ("mySerializable.namber");
  17. m_serializable_name = serializedObject.FindProperty ("mySerializable.name");
  18. }
  19.  
  20. public override void OnInspectorGUI()
  21. {
  22. //从对象抓取的最新数据
  23. serializedObject.Update ();
  24.  
  25. //属性的编辑器界面
  26. EditorGUILayout.PropertyField (m_scale);
  27. EditorGUILayout.PropertyField (m_serializable);
  28. if (m_serializable.isExpanded) {
  29. EditorGUILayout.PropertyField (m_serializable_number);
  30. EditorGUILayout.PropertyField (m_serializable_name);
  31. }
  32.  
  33. //应用属性,撤销
  34. serializedObject.ApplyModifiedProperties ();
  35. }
  36. }


s1.pngs2.png

运行效果:

s3.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号