Class SerializedSwiftDictionary<TKey, TValue>
- Namespace
- SwiftCollections.Unity
Unity-serializable adapter for persisted authoring data that is consumed as a SwiftDictionary<TKey, TValue>.
[Serializable]
public sealed class SerializedSwiftDictionary<TKey, TValue> : ISerializationCallbackReceiver where TKey : notnull
Type Parameters
TKeyA Unity-serializable dictionary key type.
TValueA Unity-serializable dictionary value type.
- Inheritance
-
SerializedSwiftDictionary<TKey, TValue>
- Implements
-
ISerializationCallbackReceiver
- Inherited Members
Remarks
Unity does not serialize dictionary fields directly, so this adapter stores an ordered array of Unity-serializable entries and rebuilds a runtime SwiftDictionary from those entries.
Properties
Count
Gets the number of entries in the runtime dictionary.
public int Count { get; }
Property Value
this[TKey]
Gets or sets a value by key and updates the serialized backing entries.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe dictionary key.
Property Value
- TValue
The value associated with the key.
Runtime
Gets the live runtime dictionary rebuilt from Unity serialized data.
public SwiftDictionary<TKey, TValue> Runtime { get; }
Property Value
- SwiftDictionary<TKey, TValue>
Remarks
Direct runtime mutations are copied into serialized backing data before Unity serialization.
Methods
Add(TKey, TValue)
Adds a key/value pair to the runtime dictionary and updates the serialized backing entries.
public bool Add(TKey key, TValue value)
Parameters
keyTKeyThe key to add.
valueTValueThe value to add.
Returns
- bool
truewhen the key was added; otherwisefalse.
Clear()
Removes all entries from the runtime dictionary and serialized backing array.
public void Clear()
ContainsKey(TKey)
Returns whether the dictionary contains the key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to look up.
Returns
- bool
trueif the key exists; otherwisefalse.
GetEnumerator()
Returns an enumerator for the runtime dictionary.
public SwiftDictionary<TKey, TValue>.SwiftDictionaryEnumerator GetEnumerator()
Returns
- SwiftDictionary<TKey, TValue>.SwiftDictionaryEnumerator
A SwiftDictionary enumerator.
OnAfterDeserialize()
Rebuilds runtime contents after Unity deserialization.
public void OnAfterDeserialize()
Exceptions
- InvalidOperationException
Serialized entries contain duplicate keys.
OnBeforeSerialize()
Copies live runtime contents into Unity's serialized backing data.
public void OnBeforeSerialize()
SetItems(params SerializedSwiftDictionaryEntry<TKey, TValue>[])
Replaces the serialized contents with a copy of the supplied entries.
public void SetItems(params SerializedSwiftDictionaryEntry<TKey, TValue>[] items)
Parameters
itemsSerializedSwiftDictionaryEntry<TKey, TValue>[]Entries to persist and expose through the runtime dictionary.
Exceptions
- ArgumentNullException
itemsis null.- InvalidOperationException
itemscontains duplicate keys.
ToArray()
Returns a copy of the serialized entries.
public SerializedSwiftDictionaryEntry<TKey, TValue>[] ToArray()
Returns
- SerializedSwiftDictionaryEntry<TKey, TValue>[]
A new array containing the current entries.
ToSwiftDictionary()
Returns a copy as a standalone SwiftDictionary<TKey, TValue>.
public SwiftDictionary<TKey, TValue> ToSwiftDictionary()
Returns
- SwiftDictionary<TKey, TValue>
A new SwiftDictionary containing the current entries.
TryGetValue(TKey, out TValue)
Attempts to get the value associated with the key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key to look up.
valueTValueThe value if found; otherwise the default value.
Returns
- bool
trueif the key exists; otherwisefalse.