Game Development Reference
In-Depth Information
Classified intel
In the Unity editor, there is an old way to access the property from the
Editor
class. We
can either use the
target
keyword to access all the properties directly from the editor or
the
SerializeProperty
class and the
SerializeObject
keyword to access the
property.
One way is to use
target
as follows:
function OnInspectorGUI() {
target.radius = EditorGUILayout.FloatField("Radius:",
target.radius);
if (GUI.changed) {
EditorUtility.SetDirty(target);
}
}
On the other hand, we use the
SerializedObject
keyword and the
Serial-
izedProperty
class to access the property, as we can see in the following code:
SerializedProperty sep_radius;
function OnEnable() {
sep_radius = serializedObject.FindProperty("radius");
}
function OnInspectorGUI() {
serializedObject.Update();
sep_radius.floatValue =
EditorGUILayout.FloatField("Radius:", sep_radius.floatValue);
serializedObject.ApplyModifiedProperties();
}
The
target
keyword can be used to access to the radius property directly by typing
target.radius
. Then, we checked if the GUI has changed or not by using
GUI.changed
. Then, we update the property by using
EditorUtil-
ity.SetDirty(target);
.
We use the
SerializedProperty
class and the
serializedObject
keyword to
set the serialize property in
OnEnable()
: