IndexedObservedCollection<T>

The IndexedObservedCollection<T> class allows you to create an observable collection where subscribers can register callback methods to be notified when the collection changes, and receive the index of the changed item. This can be useful in situations where you need to track changes to a list of items and maintain the index of each item in the list, such as when updating a UI element that displays the items in the collection.

Here is an example of how you might use the IndexedObservedCollection<T> class in a Unity project:

using System;
using UnityEngine;
using Observed;

public class MyScript : MonoBehaviour
{
    // Create an indexed observed collection of strings
    private IndexedObservedCollection<string> myCollection = new IndexedObservedCollection<string>();

    void Start()
    {
        // Subscribe to changes to the collection
        myCollection.Subscribe((item, changeType, index) =>
        {
            Debug.Log($"Item '{item}' was {changeType} at index {index}");
        });

        // Add an item to the collection
        myCollection.Add("Item 1");

        // Replace an item in the collection
        myCollection[0] = "Item 2";

        // Remove an item from the collection
        myCollection.RemoveAt(0);
    }
}

Properties

  • HandleExceptions: A flag indicating whether exceptions thrown by subscribers should be handled or allowed to propagate.

Methods

  • Subscribe(Action<T, ChangeType, int> callback): Registers a callback method to be notified when the collection changes, and receives the index of the changed item.

  • Unsubscribe(Action<T, ChangeType, int> callback): Unregisters a callback method from being notified when the collection changes.

  • ClearSubscriptions(): Removes all registered subscribers from the collection.

Inherited Members

The IndexedObservedCollection<T> class inherits all members of the ObservableCollection<T> class, including the following methods:

  • Add(T item): Adds an item to the end of the collection.

  • Clear(): Removes all elements from the collection.

  • Insert(int index, T item): Inserts an item into the collection at the specified index.

  • Remove(T item): Removes the first occurrence of the specified item from the collection.

  • RemoveAt(int index): Removes the item at the specified index from the collection.

  • Move(int oldIndex, int newIndex): Moves an item from one index to another within the collection.

  • SetItem(int index, T item): Replaces the item at the specified index with a new item.

Last updated