Version 3.3.1.2

Odin has a dedicated attribute overview with examples

OnCollectionChangedAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[DontApplyToListElements]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = true, Inherited = true)]
[Conditional("UNITY_EDITOR")]
public sealed class OnCollectionChangedAttribute : Attribute, _Attribute

OnCollectionChanged can be put on collections, and provides an event callback when the collection is about to be changed through the inspector, and when the collection has been changed through the inspector. Additionally, it provides a CollectionChangeInfo struct containing information about the exact changes made to the collection. This attribute works for all collections with a collection resolver, amongst them arrays, lists, dictionaries, hashsets, stacks and linked lists.

Inheritance
  • System.Object
  • System.Attribute
  • OnCollectionChangedAttribute
Remarks

note

Note that this attribute only works in the editor! Collections changed by script will not trigger change events!

Example

The following example shows how OnCollectionChanged can be used to get callbacks when a collection is being changed.

[OnCollectionChanged("Before", "After")]
public List<string> list;

public void Before(CollectionChangeInfo info)
{
    if (info.ChangeType == CollectionChangeType.Add || info.ChangeType == CollectionChangeType.Insert)
    {
        Debug.Log("Adding to the list!");
    }
    else if (info.ChangeType == CollectionChangeType.RemoveIndex || info.ChangeType == CollectionChangeType.RemoveValue)
    {
        Debug.Log("Removing from the list!");
    }
}

public void After(CollectionChangeInfo info)
{
    if (info.ChangeType == CollectionChangeType.Add || info.ChangeType == CollectionChangeType.Insert)
    {
        Debug.Log("Finished adding to the list!");
    }
    else if (info.ChangeType == CollectionChangeType.RemoveIndex || info.ChangeType == CollectionChangeType.RemoveValue)
    {
        Debug.Log("Finished removing from the list!");
    }
}

Constructors

OnCollectionChangedAttribute()
public OnCollectionChangedAttribute()
OnCollectionChangedAttribute(String)
public OnCollectionChangedAttribute(string after)
Parameters
System.String after

OnCollectionChangedAttribute(String, String)
public OnCollectionChangedAttribute(string before, string after)
Parameters
System.String before

System.String after

Fields

After
public string After
Before
public string Before