Skip to content

Commit

Permalink
Possibility to cast SerializedDictionary to Dictionary; minor refacto…
Browse files Browse the repository at this point in the history
…r changes
  • Loading branch information
arimger committed Mar 30, 2024
1 parent 8dd946d commit 1d4b727
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace UnityEngine
{
Expand All @@ -13,7 +13,6 @@ public sealed class SerializedDateTime : ISerializationCallbackReceiver

private DateTime dateTime;


public SerializedDateTime() : this(0)
{ }

Expand All @@ -25,7 +24,6 @@ public SerializedDateTime(DateTime dateTime)
DateTime = dateTime;
}


void ISerializationCallbackReceiver.OnAfterDeserialize()
{
dateTime = new DateTime(ticks);
Expand All @@ -45,7 +43,6 @@ public DateTime DateTime
}
}


public static implicit operator DateTime(SerializedDateTime sdt)
{
return sdt.DateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace UnityEngine
{
/// <summary>
/// Overlay for the <see cref="Dictionary{TKey, TValue}"/> to allow serialization of the key/value pairs.
/// Highly suggested to cast it to a <see cref="Dictionary{TKey, TValue}"/> when used in runtime.
/// </summary>
[Serializable]
public sealed class SerializedDictionary<TK, TV> : IDictionary<TK, TV>, ISerializationCallbackReceiver
Expand Down Expand Up @@ -38,7 +39,6 @@ public TV Value
}
}


[SerializeField]
private List<KeyValuePair> pairs = new List<KeyValuePair>();

Expand All @@ -48,6 +48,16 @@ public TV Value
[SerializeField, HideInInspector]
private bool error;

public SerializedDictionary()
{ }

public SerializedDictionary(IDictionary<TK, TV> source)
{
foreach (var pair in source)
{
Add(pair.Key, pair.Value);
}
}

private void UpdateIndexes(int removedIndex)
{
Expand All @@ -58,8 +68,8 @@ private void UpdateIndexes(int removedIndex)
}
}


void ISerializationCallbackReceiver.OnBeforeSerialize() { }
void ISerializationCallbackReceiver.OnBeforeSerialize()
{ }

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
Expand Down Expand Up @@ -122,6 +132,9 @@ public void Clear()
indexByKey.Clear();
}

/// <summary>
/// Creates a new instance of <see cref="Dictionary{TKey, TValue}"/> using the internally serialized data.
/// </summary>
public Dictionary<TK, TV> BuildNativeDictionary()
{
return new Dictionary<TK, TV>(dictionary);
Expand Down Expand Up @@ -174,7 +187,6 @@ IEnumerator IEnumerable.GetEnumerator()
return dictionary.GetEnumerator();
}


/// <summary>
/// Indicates if there is a key collision in serialized pairs.
/// Duplicated keys (pairs) won't be added to the final dictionary.
Expand All @@ -187,8 +199,6 @@ internal bool Error

public int Count => dictionary.Count;

public bool IsReadOnly => false;

public ICollection<TK> Keys => dictionary.Keys;

public ICollection<TV> Values => dictionary.Values;
Expand All @@ -211,6 +221,18 @@ public TV this[TK key]
}
}
}

public bool IsReadOnly => false;

public static implicit operator Dictionary<TK, TV>(SerializedDictionary<TK, TV> serializedDictionary)
{
return serializedDictionary.dictionary;
}

public static implicit operator SerializedDictionary<TK, TV>(Dictionary<TK, TV> dictionary)
{
return new SerializedDictionary<TK, TV>(dictionary);
}
}
}
#endif
4 changes: 0 additions & 4 deletions Assets/Editor Toolbox/Runtime/Serialization/SerializedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public sealed class SerializedType : ISerializationCallbackReceiver

private Type type;


/// <summary>
/// Initializes a new instance of the <see cref="SerializedType"/> class.
/// </summary>
Expand Down Expand Up @@ -45,7 +44,6 @@ public SerializedType(Type type)
Type = type;
}


public static string GetReferenceValue(Type type)
{
return type != null
Expand Down Expand Up @@ -86,7 +84,6 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
void ISerializationCallbackReceiver.OnBeforeSerialize()
{ }


/// <summary>
/// Gets or sets type of class reference.
/// </summary>
Expand All @@ -108,7 +105,6 @@ public Type Type
}
}


public static implicit operator string(SerializedType typeReference) => typeReference.typeReference;

public static implicit operator Type(SerializedType typeReference) => typeReference.Type;
Expand Down

0 comments on commit 1d4b727

Please sign in to comment.