Skip to content

About

Covariant observable collections for Unity with synchronization, filtering, and sorting. Pure C#, no engine dependency.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

Aspid.Collections

Minimum Unity version Stable release MIT License

Aspid.Collections provides observable collections for Unity and .NET. React to changes in an inventory or leaderboard, project models into view models, and keep filtered and sorted views up to date. The runtime is pure C# and has no Unity engine dependency.

English · Русский

Installation

Package Manager installs Aspid.Collections from its Git URL
  1. Open Window → Package Manager
  2. Choose + → Install package from git URL…
  3. Paste the URL and press Install
https://github.com/VPDPersonal/Aspid.Collections.git#upm

The URL installs the latest release. Update in the Package Manager installs the next one. To pin a version or switch channels, see Installation options.

Requires Unity 2021.3.0f1 or newer.

Troubleshooting

Compile errors right after the install. Check your Unity version in Help → About Unity (Unity → About Unity on macOS).

The Console shows Assembly with name 'Aspid.Collections.Observable' already exists. The project has a second copy of the package, for example its folder under Assets. Delete the copy.

Package Manager cannot resolve com.unity.test-framework@1.6.0. If you pinned #upm/1.1.0, switch to #upm/1.1.1 or update to the latest version on #upm. Version 1.1.1 removes this dependency. PR #20

Features

Change notifications

using var items =
    new ObservableList<string>();
items.CollectionChanged += change =>
    Console.WriteLine(change.Action);

items.Add("Potion");       // Add
items[0] = "Elixir";       // Replace
items.RemoveAt(0);         // Remove

CollectionChanged reports each edit. Use IsSingleItem to choose between the single-item and batch fields. Disposing the list emits Reset.

Basic Change Notifications →

Synchronized views

using var scores = new ObservableList<int>();
using var labels = scores.CreateSync(
    n => $"Score {n}");

scores.Add(10);
scores[0] = 20;
Console.WriteLine(labels[0]); // Score 20

CreateSync builds a read-only projection that follows the source. All five collection types support it. Overloads

Inventory Sync →

Filtering and sorting

using var scores =
    new ObservableList<int> { 25, 5, 15 };
using var visible = scores.CreateFiltered(
    n => n >= 10, Comparer<int>.Default);

scores.Add(12);
Console.WriteLine(
    string.Join(", ", visible)); // 12, 15, 25

CreateFiltered filters and sorts a view of the source. Change Filter or Comparer at runtime. Call Update() after editing a source without notifications.

Filtered Inventory →

Separate callbacks for each change

using var items =
    new ObservableList<string>();
using var events = items.SplitByEvents(
    added: (batch, _) =>
        Console.WriteLine(
            $"Added {batch.Count}"));

items.AddRange("Potion", "Elixir"); // Added 2

SplitByEvents gives each change type its own handler. Item handlers always receive a list.

Analytics Tap →

Collections

TypeUse it for
ObservableList<T>Ordered items with indexes, moves and range operations
ObservableDictionary<TKey, TValue>Items addressed by key, as in the keyed leaderboard sample
ObservableHashSet<T>Unique items and membership checks
ObservableQueue<T>First-in, first-out processing with range operations
ObservableStack<T>Last-in, first-out processing with range operations

Development

Open Aspid.Collections/ from a clone of the source repository in Unity 6000.6.5f1. Unity loads the embedded package from Packages/tech.aspid.collections/. Run the tests in the Test Runner's PlayMode tab or use dotnet test DotnetTests/Aspid.Collections.Tests.csproj from the repository root to run the unit tests outside Unity. Source repository

Resources

Unity samples Runtime API Changelog
In Package Manager, select Aspid.Collections → Samples → Import. Open the imported scene and enter Play mode. Browse collection types, event arguments and extension methods. See changes and fixes by release.

Installation options

To pin a version, use its upm/<version> tag, for example #upm/1.1.1. See the available releases.

Preview releases use the upm-preview branch when available.

Help and support

Found a bug or have a question? Include your Unity version, package version and steps to reproduce.

Open an issue · Star on GitHub

About

Covariant observable collections for Unity with synchronization, filtering, and sorting. Pure C#, no engine dependency.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages