Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


serializedproperty

SerializedProperty

Array size for multi-object editing

SerializedProperty.arraySize is said to return the smallest number of elements when editing multiple objects at the same time, but the docs are incorrect about that. It will return the array size of the first of the SerializedObject's targets, even if it's not the smallest.

On the other hand, serialized properties for array elements with an index outside the range of the smallest array size will still represent incorrect properties, so it's still important to get the smallest array size manually, or to iterate through the array's elements using Next/NextVisible.

Modifying SerializedProperty.arraySize will copy the complete array to all the edited targets, but changing the size serialized property instead will only copy the array's size to the edited objects. Example:

SerializedProperty prop = serializedObject.FindProperty("someArray");
 
// This will make each array element in the different selected targets the same. Mixed values will be lost.
prop.arraySize++
 
// This will keep the different values of the array elements in the selected targets.
// Note that targets with an array size bigger than prop.arraySize will still lose some elements.
prop.FindPropertyRelative("Array.size").intValue++
serializedproperty.txt · Last modified: 2021/10/10 22:16 by artehacker