Improve debug rendering for SerializedObject and SerializedProperty #1991
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds debugging extensions for
SerializedObject
andSerializedProperty
:Serialised Properties
node toSerializedObject
to show the root property instance and allow viewing the hierarchy of propertiesSerializedProperty
, showing name, property type and the correct value.SerializedProperty
so the correct accessors are shown for the current property type, e.g.intValue
is shown forSerializedPropertyType.Integer
,stringValue
for strings, and so on. Private or irrelevant properties are filtered out.Raw View
group toSerializedProperty
to show all fields and properties, without filtering.Children
node toSerializedProperty
to show child properties, allowing access to the full hierarchy of serialised data.SerializedProperty
to show the contents of an array orfixed
buffer. The array results will be chunked for large arrays, as per options.All values are calculated lazily, and the Unity debugger extension can already be disabled in the settings.
As viewed in the debugger:
For more background,
SerializedObject
andSerializedProperty
are the main APIs for working with Unity's serialised data - assets, scenes, prefabs and so on. Unity serialises data not just to save to disk but also to work with domain reloading, and especially editor interaction - editor windows work with serialised data rather than raw C# objects to allow for things such as change tracking and undo.The
SerializedObject
type is the root of the stream of serialised data and represents a handle on the root serialised object - asset, scene, prefab, etc. It allows for retrieving the rootSerializedProperty
, as well as access to the target C# object.The
SerializedProperty
type is a view on the stream of serialised data. It can be considered a cursor, and will be updated in-place as the data is iterated. The serialised data is hierarchical, representing a tree of properties, where a property can be a scalar value or an array, or a serializable C# class with child properties for serialised fields. However, the data is serialised as a linear stream. Iterating the stream will walk the hierarchy in a depth first fashion. CallingSerializedProperty.Next(true)
will step into a child before stepping out and on to the next sibling (Next(false)
will skip the children and move directly to the next sibling).SerializedProperty
is a discriminated union, with an enum indicating the type, and strongly typed accessor properties to retrieve the data, e.g.intValue
,stringValue
,vector2Value
, etc.