序列化对象 与 自定义Inspector属性面板
一、创建MyTransform.cs、TransformInspector.cs
using UnityEngine; using System.Collections; public class MyTransform : MonoBehaviour { public MySerializable mySerializable; [SerializeField] private float mscale; void OnEnable() { Debug.Log ("mscale="+mscale); Debug.Log ("mySerializable.namber="+mySerializable.namber); Debug.Log ("mySerializable.name="+mySerializable.name); } } [System.Serializable] public class MySerializable { public int namber; public string name; }
//自定义MyTransform组件的检视面板 using UnityEngine; using UnityEditor; [CanEditMultipleObjects] [CustomEditor(typeof(MyTransform))] public class TransformInspector : Editor { SerializedProperty m_scale; SerializedProperty m_serializable; SerializedProperty m_serializable_number; SerializedProperty m_serializable_name; void OnEnable () { m_scale = serializedObject.FindProperty ("mscale"); m_serializable = serializedObject.FindProperty ("mySerializable"); m_serializable_number = serializedObject.FindProperty ("mySerializable.namber"); m_serializable_name = serializedObject.FindProperty ("mySerializable.name"); } public override void OnInspectorGUI() { //从对象抓取的最新数据 serializedObject.Update (); //属性的编辑器界面 EditorGUILayout.PropertyField (m_scale); EditorGUILayout.PropertyField (m_serializable); if (m_serializable.isExpanded) { EditorGUILayout.PropertyField (m_serializable_number); EditorGUILayout.PropertyField (m_serializable_name); } //应用属性,撤销 serializedObject.ApplyModifiedProperties (); } }
运行效果: