ScriptableDictionary<TKey, TValue> Class
Definition
Section titled “Definition”A UnityEngine.ScriptableObject that holds a Dictionary<TKey, TValue>.
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 : notnullType Parameters
Section titled “Type Parameters”TKey
The key of the dictionary.
TValue
The value of the dictionary.
Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObject → ScriptableDictionary
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
Properties
Section titled “Properties”this[TKey]
Section titled “this[TKey]”public TValue this[TKey key] { get; set; }Comparer
Section titled “Comparer”Gets or sets the IEqualityComparer<T> that is used to determine equality of keys for the ScriptableDictionary<TKey, TValue>.
public IEqualityComparer<TKey> Comparer { get; set; }Gets the number of key/value pairs contained in the ScriptableDictionary<TKey, TValue>.
public override sealed int Count { get; protected set; }IsReadOnly
Section titled “IsReadOnly”If read only, the ScriptableDictionary<TKey, TValue> cannot be changed at runtime and won’t be cleared on start.
public override bool IsReadOnly { get; set; }SetEqualityCheck
Section titled “SetEqualityCheck”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.
public override bool SetEqualityCheck { get; set; }ClearOnStart
Section titled “ClearOnStart”If true, the ScriptableDictionary<TKey, TValue> will be cleared on play mode start/game boot.
public override bool ClearOnStart { get; set; }Gets a collection containing the keys in the dictionary.
public Dictionary<TKey, TValue>.KeyCollection Keys { get; }Values
Section titled “Values”Gets a collection containing the values in the ScriptableDictionary<TKey, TValue>.
public Dictionary<TKey, TValue>.ValueCollection Values { get; }Methods
Section titled “Methods”Add(TKey, TValue)
Section titled “Add(TKey, TValue)”Adds the specified key and value to the ScriptableDictionary<TKey, TValue>.
public void Add(TKey key, TValue value)Parameters
Section titled “Parameters”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.
Exceptions
Section titled “Exceptions”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.
Clear()
Section titled “Clear()”Removes all keys and values from the ScriptableDictionary<TKey, TValue>.
public override sealed void Clear()Exceptions
Section titled “Exceptions”ReadOnlyException
If the object is read-only and the application is playing.
ContainsKey(TKey)
Section titled “ContainsKey(TKey)”Determines whether the ScriptableDictionary<TKey, TValue> contains the specified key.
public bool ContainsKey(TKey key)Parameters
Section titled “Parameters”key TKey
The key to locate in the
Returns
Section titled “Returns”bool
true if the ScriptableDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
Section titled “Exceptions”ArgumentNullException
key is null.
ContainsValue(TValue)
Section titled “ContainsValue(TValue)”Determines whether the ScriptableDictionary<TKey, TValue> contains a specific value.
public bool ContainsValue(TValue value)Parameters
Section titled “Parameters”value TValue
The value to locate in the null for reference types.
Returns
Section titled “Returns”bool
true if the ScriptableDictionary<TKey, TValue> contains the value; otherwise, false.
EnsureCapacity(int)
Section titled “EnsureCapacity(int)”Ensures that the dictionary can hold up to a specified number of entries without any further expansion of its backing storage.
public override sealed int EnsureCapacity(int capacity)Parameters
Section titled “Parameters”capacity int
The number of entries.
Returns
Section titled “Returns”int
The current capacity of the ScriptableDictionary<TKey, TValue>.
Exceptions
Section titled “Exceptions”ArgumentOutOfRangeException
capacity is less than 0.
GetEnumerator()
Section titled “GetEnumerator()”Returns an enumerator that iterates through the ScriptableDictionary<TKey, TValue>.
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()Returns
Section titled “Returns”Dictionary<TKey, TValue>.Enumerator
A Dictionary<TKey, TValue>.Enumerator structure for the ScriptableDictionary<TKey, TValue>.
Remove(TKey)
Section titled “Remove(TKey)”Removes the value with the specified key from the ScriptableDictionary<TKey, TValue>.
public bool Remove(TKey key)Parameters
Section titled “Parameters”key TKey
The key of the element to remove.
Returns
Section titled “Returns”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>.
Exceptions
Section titled “Exceptions”ArgumentNullException
key is null.
ReadOnlyException
If the object is read-only and the application is playing.
Remove(TKey, out TValue)
Section titled “Remove(TKey, out TValue)”Removes the value with the specified key from the ScriptableDictionary<TKey, TValue>, and copies the element to the value parameter.
public bool Remove(TKey key, out TValue value)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”bool
true if the element is successfully found and removed; otherwise, false.
Exceptions
Section titled “Exceptions”ArgumentNullException
key is null.
ReadOnlyException
If the object is read-only and the application is playing.
TrimExcess()
Section titled “TrimExcess()”Sets the capacity of this ScriptableDictionary<TKey, TValue> to what it would be if it had been originally initialized with all its entries.
public override sealed void TrimExcess()TrimExcess(int)
Section titled “TrimExcess(int)”Sets the capacity of this ScriptableDictionary<TKey, TValue> to hold up a specified number of entries without any further expansion of its backing storage.
public override sealed void TrimExcess(int capacity)Parameters
Section titled “Parameters”capacity int
The new capacity.
Exceptions
Section titled “Exceptions”ArgumentOutOfRangeException
capacity is less than the number of items in the ScriptableDictionary<TKey, TValue>.
TryAdd(TKey, TValue)
Section titled “TryAdd(TKey, TValue)”Attempts to add the specified key and value to the ScriptableDictionary<TKey, TValue>.
public bool TryAdd(TKey key, TValue value)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”bool
true if the key/value pair was added to the ScriptableDictionary<TKey, TValue> successfully; false if the key already exists.
Exceptions
Section titled “Exceptions”ArgumentNullException
key is null.
ReadOnlyException
If the object is read-only and the application is playing.
TryFindKey(Predicate, out TKey?)
Section titled “TryFindKey(Predicate, out TKey?)”Tries to find a key in the ScriptableDictionary<TKey, TValue> that matches the specified predicate.
public bool TryFindKey(Predicate<TKey> predicate, out TKey? key)Parameters
Section titled “Parameters”predicate Predicate<TKey>
The
key TKey
When this method returns, contains the key if it was found. Will be the default value if it wasn’t
found.
Returns
Section titled “Returns”bool
true if a key matching the predicate was found; otherwise, false.
Exceptions
Section titled “Exceptions”ArgumentNullException
predicate is null.
TryFindValue(Predicate, out TValue?)
Section titled “TryFindValue(Predicate, out TValue?)”Tries to find a value in the ScriptableDictionary<TKey, TValue> that matches the specified predicate.
public bool TryFindValue(Predicate<TValue> predicate, out TValue? value)Parameters
Section titled “Parameters”predicate Predicate<TValue>
The
value TValue
When this method returns, contains the value if it was found. Will be the default value if it
wasn’t found.
Returns
Section titled “Returns”bool
true if a value matching the predicate was found; otherwise, false.
Exceptions
Section titled “Exceptions”ArgumentNullException
predicate is null.
TryGetValue(TKey, out TValue)
Section titled “TryGetValue(TKey, out TValue)”Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”bool
true if the ScriptableDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
Section titled “Exceptions”ArgumentNullException
key is null.
OnPreStart()
Section titled “OnPreStart()”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.
protected override void OnPreStart()Remarks
Section titled “Remarks”If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.
WarnIfLeftOverSubscribers()
Section titled “WarnIfLeftOverSubscribers()”Warns if there are any left-over subscribers to the events.
protected override void WarnIfLeftOverSubscribers()Remarks
Section titled “Remarks”This will only be called in the Unity editor and builds with the DEBUG flag.
WarnIfLeftOverObjects()
Section titled “WarnIfLeftOverObjects()”Warns if there are any left-over objects in the dictionary.
[Conditional("DEBUG")]protected void WarnIfLeftOverObjects()Remarks
Section titled “Remarks”This will only be called in the Unity editor and builds with the DEBUG flag.
ClearSubscribers(bool)
Section titled “ClearSubscribers(bool)”Removes any subscribers from the event.
public void ClearSubscribers(bool warnIfLeftOver = false)Parameters
Section titled “Parameters”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.
OnDisabled()
Section titled “OnDisabled()”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.
protected override void OnDisabled()Remarks
Section titled “Remarks”This can also be called during the game is the scriptable object is destroyed and/or unloaded.
Events
Section titled “Events”OnCollectionChanged
Section titled “OnCollectionChanged”Occurs when an item is added, removed, replaced, or the entire ScriptableDictionary<TKey, TValue> is refreshed.
public event CollectionChangedEventHandler<KeyValuePair<TKey, TValue>>? OnCollectionChangedEvent Type
Section titled “Event Type”CollectionChangedEventHandler<KeyValuePair<TKey, TValue>>
OnAdded
Section titled “OnAdded”Called when an item was added. Gives you the key and value of the newly added item.
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]public event Action<TKey, TValue>? OnAddedEvent Type
Section titled “Event Type”Called when an item was set. Gives you the key, the old value, and the new value of the item.
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]public event Action<TKey, TValue, TValue>? OnSetEvent Type
Section titled “Event Type”OnRemoved
Section titled “OnRemoved”Called when an item was removed. Gives you the key and value of the removed item.
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]public event Action<TKey, TValue>? OnRemovedEvent Type
Section titled “Event Type”OnCleared
Section titled “OnCleared”Called when the dictionary is cleared.
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]public event Action? OnClearedEvent Type
Section titled “Event Type”OnChanged
Section titled “OnChanged”Called when the dictionary is changed in any way.
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]public event Action<DictionaryChangeType>? OnChanged