Table of Contents

Class SerializedSwiftDictionary<TKey, TValue>

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

TKey

A Unity-serializable dictionary key type.

TValue

A 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

int

this[TKey]

Gets or sets a value by key and updates the serialized backing entries.

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

Parameters

key TKey

The 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

key TKey

The key to add.

value TValue

The value to add.

Returns

bool

true when the key was added; otherwise false.

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

key TKey

The key to look up.

Returns

bool

true if the key exists; otherwise false.

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

items SerializedSwiftDictionaryEntry<TKey, TValue>[]

Entries to persist and expose through the runtime dictionary.

Exceptions

ArgumentNullException

items is null.

InvalidOperationException

items contains 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

key TKey

The key to look up.

value TValue

The value if found; otherwise the default value.

Returns

bool

true if the key exists; otherwise false.