ObservedCollection<T>

The ObservedCollection is a subclass of ObservableCollection that allows subscribing to changes in the collection. It provides a more fine-grained control over the types of changes that can be subscribed to (e.g. item added, item removed, item updated), as well as the ability to subscribe and unsubscribe to these change notifications.

Usage

Here is an example of using the ObservedCollection class:

ObservedCollection<string> observedCollection = new ObservedCollection<string>();

observedCollection.Subscribe((item, changeType) =>
{
    switch (changeType)
    {
        case ChangeType.Added:
            Debug.Log($"Item '{item}' was added to the collection.");
            break;
        case ChangeType.Removed:
            Debug.Log($"Item '{item}' was removed from the collection.");
            break;
        case ChangeType.Updated:
            Debug.Log($"Item '{item}' was updated in the collection.");
            break;
    }
});

observedCollection.Add("apple"); 
// Output: "Item 'apple' was added to the collection."
observedCollection[0] = "banana"; 
// Output: "Item 'banana' was updated in the collection."
observedCollection.Remove("banana"); 
// Output: "Item 'banana' was removed from the collection."

observedCollection.Add("cat");
observedCollection.Add("dog");
observedCollection.Clear(); 
// Output: "Item 'cat' was removed from the collection."
//"Item 'dog' was removed from the collection."

observedCollection.Add("elephant"); 
// Output: "Item 'elephant' was added to the collection."
observedCollection[0] = "elephant"; 
// No output

bool contains = observedCollection.Contains("elephant"); // True

int count = observedCollection.Count; // 1

string[] array = new string[1];
observedCollection.CopyTo(array, 0); 
// array[0] is now "elephant"

observedCollection[0] = "giraffe";
observedCollection.Move(0, 1); 
// Output: "Item 'giraffe' was updated in the collection."

observedCollection.Insert(0, "hippopotamus"); 
// Output: "Item 'hippopotamus' was

Getting the index of an item

Here is how you can get the index of a changed item:

using Observed;
using UnityEngine;

public class MyClass : MonoBehaviour
{
    private ObservedCollection<GameObject> collection = new ObservedCollection<GameObject>();

    private void Start()
    {
        collection.Subscribe(OnCollectionChanged);
    }

    private void OnCollectionChanged(GameObject item, ChangeType type)
    {
        int index = collection.IndexOf(item);
    }
}

If you have duplicates in your collections, you might want to use IndexedObservedCollection instead which will notify subscribers with the index of the item.

Properties

  • SubscriberCount: the number of subscribers currently listening for change notifications.

Constructors

  • ObservedCollection(): constructs an empty ObservedCollection<T>.

Methods

  • Subscribe(Action<T, ChangeType> callback): subscribes the given callback to be notified of changes to the collection.

  • Unsubscribe(Action<T, ChangeType> callback): removes the given callback from the list of subscribers.

  • Clear(): removes all items from the collection and sends a change notification to all subscribers for each removed item.

  • ClearSubscriptions(): removes all subscribers from the list of subscribers.

Last updated