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.
Note that this attribute only works in the editor! Collections changed by script will not trigger change events!
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!");
}
}
public OnCollectionChangedAttribute()
public OnCollectionChangedAttribute(string after)
System.String | after |
public OnCollectionChangedAttribute(string before, string after)
System.String | before | |
System.String | after |
public string After
public string Before