Repository navigation
Features Utilities Reference Identity
ReferenceIdentityComparer<T> compares managed references with ReferenceEquals
and hashes them with RuntimeHelpers.GetHashCode. It does not call a key's
Equals or GetHashCode override. Use its shared Instance in dictionaries and
sets when value equality would hide distinct objects.
using System.Collections.Generic;
using UnityEngine;
using WallstopStudios.UnityHelpers.Core.Helper;
Dictionary<GameObject, int> counts = new(
ReferenceIdentityComparer<GameObject>.Instance);A destroyed Unity object still has a managed reference. That reference remains a
valid lookup or removal key, and two different destroyed objects remain distinct.
The comparer accepts null: two managed null references compare equal and null
hashes to zero. A normal Dictionary still refuses null keys.
UnityObjectMap<TKey, TValue> in WallstopStudios.UnityHelpers.Utils uses the
comparer and provides explicit cleanup. Use all map operations on Unity's main
thread. It is not a concurrent collection.
using WallstopStudios.UnityHelpers.Utils;
using UnityObjectMap<GameObject, int> counts = new(autoPruneOnSceneUnload: true);
counts.TrySet(source, 1);
if (counts.TryGetValue(source, out int count))
{
UnityEngine.Debug.Log(count);
}
int removed = counts.PruneDestroyed();-
TrySetinserts or replaces an entry. Managed null keys are refused. Values can be null, and destroyed keys can be stored for diagnostics. -
TryGetValue,ContainsKeyandRemoveask about the exact managed reference, including destroyed objects.Countincludes entries awaiting cleanup. -
PruneDestroyedremoves exactly the destroyed keys and returns their count. Live keys remain. Its temporary list comes fromBuffers<TKey>.List. -
Clearreleases all entries.Disposealso disables automatic pruning. Disposal is idempotent; later insertion and lookup return false.
foreach over the typed map yields only live keys. It does not remove destroyed
entries, so Count can be larger than the number of pairs yielded. The typed
struct enumerator allocates no managed storage. Enumeration through IEnumerable
boxes the enumerator. Destroying a key during traversal skips that key when it is
next reached. Replacing or inserting a value, removing an entry, clearing,
pruning entries, or disposing the map invalidates an existing enumerator;
MoveNext and Reset then throw InvalidOperationException.
GetOrAdd(key, factory) returns an existing live key's value, including null.
On a miss it calls factory(key) once and stores the result. The overload
GetOrAdd(key, factory, state) passes caller state to a static factory without
requiring a captured delegate. Managed null keys, null factories, and disposed
maps return default without calling a factory.
A destroyed key is a miss on every call. Its old value is replaced with the
factory result under the same managed reference. It remains invisible to
iteration and eligible for PruneDestroyed. A live value inserted by a factory
wins over its returned value. Disposal during a factory refuses publication and
returns default. An exception from caller factory code propagates; the map does
not publish a result. Changes made by the factory itself remain its responsibility.
Set AutoPruneOnSceneUnload to opt in or out. A single cached scene-unload
handler services all enrolled maps. Weak registration lets an abandoned map and
its values be collected. Repeated opt-in does not add another subscription.
At SubsystemRegistration, enrolled maps are cleared, their opt-in flags are
reset, and the handler is removed. This prevents previous play-session state
from surviving when domain reload is disabled. Enable automatic pruning again
when initializing each play session. Maps that never opt in stay entirely
manual, including session cleanup.
Use the comparer when distinct managed objects must remain distinct keys, including destroyed
Unity objects retained for diagnostics. Use UnityObjectMap when you also need explicit or
scene-unload cleanup. Neither changes Unity's overloaded null checks elsewhere in your code.
📦 Unity Helpers | 📖 Documentation | 🐛 Issues | 📜 MIT License
- Inspector Button
- Inspector Conditional Display
- Inspector Grouping Attributes
- Inspector Inline Editor
- Inspector Overview
- Inspector Selection Attributes
- Inspector Settings
- Inspector Validation Attributes
- Utility Components
- Visual Components
- Data Structures
- Helper Utilities
- Math And Extensions
- Pooling Guide
- Random Generators
- Reference Identity
- Reflection Helpers
- Singletons
- Asset Change Detection
- Asset Validation
- Authored Asset Validation
- Build Profile Scenes
- Editor Tools Guide
- Failed Tests Exporter
- Scene Canonicalization
- Sprite Animation Motion
- Test Run Reporter
- Unity Method Analyzer
- Ai Model Backends
- Bundled Assembly Conflicts
- Devcontainer Persistence
- Mcp Ecosystem
- Mcp Local Setup
- Odin Migration Guide
- Unity Devcontainer Licensing
- Agent Iteration
- Analyzers
- Baseline Tests Performance
- Bendable Line Sampling
- Bit Lookup Tables
- Dictionary Key Performance
- Enum Helpers Performance
- Enumeration Audit
- Ilist Sorting Performance
- Json Codegen Performance
- Local Pools
- Ownership Audit
- Random Performance
- Reflection Performance
- Performance
- Singleton Policy Fixtures
- Tree 2d Performance
- Tree 3d Performance
- Sprite Bounds Row Scan
- Sprite Ink Traversal
- Sprite Region Read
- Synchronous Test Lifecycle