Skip to main content

SerializedProperty Extensions

Write values and access C# fields through SerializedProperty.

Quick start​

Before — Unity APIAfter — FastTools
serializedObject.Update();
manaCost.intValue = 42;
serializedObject
.ApplyModifiedProperties();
manaCost
.Update()
.SetIntAndApply(42);

Writing values​

CallWhat it does
SetInt(42)Writes the value
SetIntAndApply(42)Writes the value and applies changes
SetIntAndApplyWithoutUndo(42)Writes the value and applies changes without Undo

The SetValue overloads select a setter by argument type: SetValue(42) calls SetInt. The argument type must match the field type. Integer types narrower than int, and char, also call SetInt. See the API reference for the full list of setters.

Enums, arrays and references​

UnityFastTools
intValue = (int)Rarity.RareSetEnum(Rarity.Rare)
enumValueIndex = 1SetEnumIndex(1)
enumValueFlag = flagsSetEnumFlag(flags)
arraySize = 5SetArraySize(5)
arraySize++AddArraySize()
arraySize--RemoveArraySize()
managedReferenceValue = valueSetManagedReference(value)
objectReferenceValue = valueSetObjectReference(value)
boxedValue = valueSetBoxed(value)
exposedReferenceValue = valueSetExposedReference(value)
  • SetEnumIndex and SetEnumFlag have no SetValue form: call them by name.
  • SetValue(1) on an enum field calls SetInt: it writes the value 1, not the index.
  • SetEnum and SetValue(Rarity.Rare) write the enum value.
warning

With an IExposedPropertyTable context, such as a PlayableDirector, SetExposedReference() stores the reference in the table at once, but the new name stays pending. Call ApplyModifiedProperties() after it: otherwise the next update or a domain reload discards the name and leaves an orphan entry in the table.

Update() and Apply…()​

These methods are called on the property and, like setters, return it for chaining.

UnityFastTools
serializedObject.Update()Update()
serializedObject.UpdateIfRequiredOrScript()UpdateIfRequiredOrScript()
serializedObject.ApplyModifiedProperties()ApplyModifiedProperties()
serializedObject.ApplyModifiedPropertiesWithoutUndo()ApplyModifiedPropertiesWithoutUndo()

Accessing the C# field​

The methods return the field type, its FieldInfo and the owning object.

public class AbilityBook : MonoBehaviour
{
[SerializeField]
private List<Ability> _abilities = new() { new Ability() };

[SerializeReference]
private IAbilityEffect _effect = new BurnEffect();
}

[Serializable]
public class Ability { public string Name = "Fireball"; }

[Serializable]
public class BurnEffect : IAbilityEffect { public float Damage = 5f; }
Field / elementGetPropertyType()GetFieldInfo()GetDeclaringInstance()
_abilitiesList<Ability>AbilityBook._abilitiesthe AbilityBook
_abilities[0]AbilityAbilityBook._abilitiesthe AbilityBook
_abilities[0].NamestringAbility.Namethe Ability at index 0
_effectIAbilityEffectAbilityBook._effectthe AbilityBook
_effect.DamagefloatBurnEffect.Damagethe BurnEffect instance
  • if the field is not found, GetPropertyType() and GetFieldInfo() return null, and GetDeclaringInstance() does only when the owning object is not found;
  • the methods read only the first target object and only applied values.
warning

When the field's owner is a struct, GetDeclaringInstance() returns a boxed copy. Changes to that copy never reach the original; write values through the SerializedProperty instead.

Member name and drawing checks​

Field / elementGetMemberName()IsArrayElement()HasFoldout()
_abilities[0]"_abilities"truetrue
_abilities[0].Name"Name"falsefalse
_effect"_effect"falsefalse
note

HasFoldout() checks ordinary compound fields with visible children. It does not account for [SerializeReference] or custom PropertyDrawers.

Persistent()​

Persistent() returns a copy of the property on its own SerializedObject, so it can be used later, for example in EditorApplication.delayCall.

  • the copy belongs to the caller: dispose it together with its serializedObject;
  • unapplied writes of the source property do not reach the copy;
  • if the property is missing on the targets, the method returns null.
var independent = manaCost.Persistent();
if (independent == null) return;

EditorApplication.delayCall += () =>
{
using (independent.serializedObject)
using (independent)
independent.Update().SetIntAndApply(42);
};

Package sample​

In EditorTools, the Reset to defaults button writes two properties with SetFloat and SetIntAndApply.

The Reset to defaults button returns the cost and the cooldown to their defaults, and Undo restores them.The Reset to defaults button returns the cost and the cooldown to their defaults, and Undo restores them.