Reference identity and Unity object maps¶
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.
| C# | |
|---|---|
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.
Pruning destroyed Unity 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.
| C# | |
|---|---|
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.
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.