Skip to content

ScriptableDictionary<TKey, TValue> Class

A UnityEngine.ScriptableObject that holds a Dictionary<TKey, TValue>.

C#
public abstract class ScriptableDictionary<TKey, TValue> : ScriptableDictionary, INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly, INotifyCollectionChanged, ISerializationCallbackReceiver, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, INotifyScriptableCollectionChanged<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable where TKey : notnull

TKey
The key of the dictionary.

TValue
The value of the dictionary.

Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObjectScriptableDictionary

Implements INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly, INotifyCollectionChanged, UnityEngine.ISerializationCallbackReceiver, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, INotifyScriptableCollectionChanged<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

C#
public TValue this[TKey key] { get; set; }

Gets or sets the IEqualityComparer<T> that is used to determine equality of keys for the ScriptableDictionary<TKey, TValue>.

C#
public IEqualityComparer<TKey> Comparer { get; set; }

Gets the number of key/value pairs contained in the ScriptableDictionary<TKey, TValue>.

C#
public override sealed int Count { get; protected set; }

If read only, the ScriptableDictionary<TKey, TValue> cannot be changed at runtime and won’t be cleared on start.

C#
public override bool IsReadOnly { get; set; }

If true, an equality check will be run before setting an item through the indexer to make sure the new object is not the same as the old one.

C#
public override bool SetEqualityCheck { get; set; }

If true, the ScriptableDictionary<TKey, TValue> will be cleared on play mode start/game boot.

C#
public override bool ClearOnStart { get; set; }

Gets a collection containing the keys in the dictionary.

C#
public Dictionary<TKey, TValue>.KeyCollection Keys { get; }

Gets a collection containing the values in the ScriptableDictionary<TKey, TValue>.

C#
public Dictionary<TKey, TValue>.ValueCollection Values { get; }

Adds the specified key and value to the ScriptableDictionary<TKey, TValue>.

C#
public void Add(TKey key, TValue value)

key TKey
The key of the element to add.

value TValue
The value of the element to add. The value can be null for reference types.

ArgumentNullException
key is null.

ArgumentException
An element with the same key already exists in the ScriptableDictionary<TKey, TValue>

ReadOnlyException
If the object is read-only and the application is playing.

Removes all keys and values from the ScriptableDictionary<TKey, TValue>.

C#
public override sealed void Clear()

ReadOnlyException
If the object is read-only and the application is playing.

Determines whether the ScriptableDictionary<TKey, TValue> contains the specified key.

C#
public bool ContainsKey(TKey key)

key TKey
The key to locate in the .

bool
true if the ScriptableDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.

ArgumentNullException
key is null.

Determines whether the ScriptableDictionary<TKey, TValue> contains a specific value.

C#
public bool ContainsValue(TValue value)

value TValue
The value to locate in the . The value can be null for reference types.

bool
true if the ScriptableDictionary<TKey, TValue> contains the value; otherwise, false.

Ensures that the dictionary can hold up to a specified number of entries without any further expansion of its backing storage.

C#
public override sealed int EnsureCapacity(int capacity)

capacity int
The number of entries.

int
The current capacity of the ScriptableDictionary<TKey, TValue>.

ArgumentOutOfRangeException
capacity is less than 0.

Returns an enumerator that iterates through the ScriptableDictionary<TKey, TValue>.

C#
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()

Dictionary<TKey, TValue>.Enumerator
A Dictionary<TKey, TValue>.Enumerator structure for the ScriptableDictionary<TKey, TValue>.

Removes the value with the specified key from the ScriptableDictionary<TKey, TValue>.

C#
public bool Remove(TKey key)

key TKey
The key of the element to remove.

bool
true if the element is successfully found and removed; otherwise, false. This method returns false if the key is not found in the ScriptableDictionary<TKey, TValue>.

ArgumentNullException
key is null.

ReadOnlyException
If the object is read-only and the application is playing.

Removes the value with the specified key from the ScriptableDictionary<TKey, TValue>, and copies the element to the value parameter.

C#
public bool Remove(TKey key, out TValue value)

key TKey
The key of the element to remove.

value TValue
When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

bool
true if the element is successfully found and removed; otherwise, false.

ArgumentNullException
key is null.

ReadOnlyException
If the object is read-only and the application is playing.

Sets the capacity of this ScriptableDictionary<TKey, TValue> to what it would be if it had been originally initialized with all its entries.

C#
public override sealed void TrimExcess()

Sets the capacity of this ScriptableDictionary<TKey, TValue> to hold up a specified number of entries without any further expansion of its backing storage.

C#
public override sealed void TrimExcess(int capacity)

capacity int
The new capacity.

ArgumentOutOfRangeException
capacity is less than the number of items in the ScriptableDictionary<TKey, TValue>.

Attempts to add the specified key and value to the ScriptableDictionary<TKey, TValue>.

C#
public bool TryAdd(TKey key, TValue value)

key TKey
The key of the element to add.

value TValue
The value of the element to add. The value can be null for reference types.

bool
true if the key/value pair was added to the ScriptableDictionary<TKey, TValue> successfully; false if the key already exists.

ArgumentNullException
key is null.

ReadOnlyException
If the object is read-only and the application is playing.

Tries to find a key in the ScriptableDictionary<TKey, TValue> that matches the specified predicate.

C#
public bool TryFindKey(Predicate<TKey> predicate, out TKey? key)

predicate Predicate<TKey>
The to check against.

key TKey
When this method returns, contains the key if it was found. Will be the default value if it wasn’t found.

bool
true if a key matching the predicate was found; otherwise, false.

ArgumentNullException
predicate is null.

Tries to find a value in the ScriptableDictionary<TKey, TValue> that matches the specified predicate.

C#
public bool TryFindValue(Predicate<TValue> predicate, out TValue? value)

predicate Predicate<TValue>
The to check against.

value TValue
When this method returns, contains the value if it was found. Will be the default value if it wasn’t found.

bool
true if a value matching the predicate was found; otherwise, false.

ArgumentNullException
predicate is null.

Gets the value associated with the specified key.

C#
public bool TryGetValue(TKey key, out TValue value)

key TKey
The key of the value to get.

value TValue
When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

bool
true if the ScriptableDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.

ArgumentNullException
key is null.

Called when the game starts, before OnStart().

You should not subscribe to events in this method! Use OnStart() instead. OnPreStart() should be used for preparing the scriptable object.

C#
protected override void OnPreStart()

If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.

Warns if there are any left-over subscribers to the events.

C#
protected override void WarnIfLeftOverSubscribers()

This will only be called in the Unity editor and builds with the DEBUG flag.

Warns if there are any left-over objects in the dictionary.

C#
[Conditional("DEBUG")]
protected void WarnIfLeftOverObjects()

This will only be called in the Unity editor and builds with the DEBUG flag.

Removes any subscribers from the event.

C#
public void ClearSubscribers(bool warnIfLeftOver = false)

warnIfLeftOver bool
If true, a warning will be printed in the console if there are any subscribers. The warning will only be printed in the editor and debug builds.

Called when the game is ending, after OnPreDisabled().

You should not unsubscribe from events in this method! Use OnPreDisabled() instead. OnDisabled() should be used for cleaning up the scriptable object.

C#
protected override void OnDisabled()

This can also be called during the game is the scriptable object is destroyed and/or unloaded.

Occurs when an item is added, removed, replaced, or the entire ScriptableDictionary<TKey, TValue> is refreshed.

C#
public event CollectionChangedEventHandler<KeyValuePair<TKey, TValue>>? OnCollectionChanged

CollectionChangedEventHandler<KeyValuePair<TKey, TValue>>

Called when an item was added. Gives you the key and value of the newly added item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<TKey, TValue>? OnAdded

Action<TKey, TValue>

Called when an item was set. Gives you the key, the old value, and the new value of the item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<TKey, TValue, TValue>? OnSet

Action<TKey, TValue, TValue>

Called when an item was removed. Gives you the key and value of the removed item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<TKey, TValue>? OnRemoved

Action<TKey, TValue>

Called when the dictionary is cleared.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action? OnCleared

Action

Called when the dictionary is changed in any way.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<DictionaryChangeType>? OnChanged

Action<DictionaryChangeType>