SerializedObject 序列化物体

SerializedObject and SerializedProperty are classes for editing properties on objects in a completely generic way that automatically handles undo and styling UI for prefabs.

SerializedObject 和SerializedProperty 是编辑属性在物体上以完全常见的方式的类,它自动的控制为prefabs(预设)undo( 撤消)和风格UI(用户界面)。

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.
注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。

SerializedObject is used in conjunction with SerializedProperty and Editor classes.

SerializedObject连同SerializedProperty 和编辑器类一起用。

// C# example: A custom Inspector for Transform components.
using UnityEngine;
using UnityEditor;

public class TransformInspector : Editor {

	SerializedObject m_Object;
	SerializedProperty m_Property;

	void OnEnable () {
		m_Object = new SerializedObject (target);
		m_Property = m_Object.FindProperty ("m_LocalPosition.x");

	void OnInspectorGUI () {
		// Grab the latest data from the object
		// 从物体上抓取最新的数据。
		m_Object.Update ();

		// Editor UI for the property
		// 属性的编辑器UI(用户界面)
		EditorGUILayout.PropertyField (m_Property);

		// Apply the property, handle undo
		// 应用属性,处理undo(撤销)。
		m_Object.ApplyModifiedProperties ();




Page last updated: 2011-6-17