Skip to content

Commit 1d4b727

Browse files
committed
Possibility to cast SerializedDictionary to Dictionary; minor refactor changes
1 parent 8dd946d commit 1d4b727

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

Assets/Editor Toolbox/Runtime/Serialization/SerializedDateTime.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace UnityEngine
44
{
@@ -13,7 +13,6 @@ public sealed class SerializedDateTime : ISerializationCallbackReceiver
1313

1414
private DateTime dateTime;
1515

16-
1716
public SerializedDateTime() : this(0)
1817
{ }
1918

@@ -25,7 +24,6 @@ public SerializedDateTime(DateTime dateTime)
2524
DateTime = dateTime;
2625
}
2726

28-
2927
void ISerializationCallbackReceiver.OnAfterDeserialize()
3028
{
3129
dateTime = new DateTime(ticks);
@@ -45,7 +43,6 @@ public DateTime DateTime
4543
}
4644
}
4745

48-
4946
public static implicit operator DateTime(SerializedDateTime sdt)
5047
{
5148
return sdt.DateTime;

Assets/Editor Toolbox/Runtime/Serialization/SerializedDictionary.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace UnityEngine
77
{
88
/// <summary>
99
/// Overlay for the <see cref="Dictionary{TKey, TValue}"/> to allow serialization of the key/value pairs.
10+
/// Highly suggested to cast it to a <see cref="Dictionary{TKey, TValue}"/> when used in runtime.
1011
/// </summary>
1112
[Serializable]
1213
public sealed class SerializedDictionary<TK, TV> : IDictionary<TK, TV>, ISerializationCallbackReceiver
@@ -38,7 +39,6 @@ public TV Value
3839
}
3940
}
4041

41-
4242
[SerializeField]
4343
private List<KeyValuePair> pairs = new List<KeyValuePair>();
4444

@@ -48,6 +48,16 @@ public TV Value
4848
[SerializeField, HideInInspector]
4949
private bool error;
5050

51+
public SerializedDictionary()
52+
{ }
53+
54+
public SerializedDictionary(IDictionary<TK, TV> source)
55+
{
56+
foreach (var pair in source)
57+
{
58+
Add(pair.Key, pair.Value);
59+
}
60+
}
5161

5262
private void UpdateIndexes(int removedIndex)
5363
{
@@ -58,8 +68,8 @@ private void UpdateIndexes(int removedIndex)
5868
}
5969
}
6070

61-
62-
void ISerializationCallbackReceiver.OnBeforeSerialize() { }
71+
void ISerializationCallbackReceiver.OnBeforeSerialize()
72+
{ }
6373

6474
void ISerializationCallbackReceiver.OnAfterDeserialize()
6575
{
@@ -122,6 +132,9 @@ public void Clear()
122132
indexByKey.Clear();
123133
}
124134

135+
/// <summary>
136+
/// Creates a new instance of <see cref="Dictionary{TKey, TValue}"/> using the internally serialized data.
137+
/// </summary>
125138
public Dictionary<TK, TV> BuildNativeDictionary()
126139
{
127140
return new Dictionary<TK, TV>(dictionary);
@@ -174,7 +187,6 @@ IEnumerator IEnumerable.GetEnumerator()
174187
return dictionary.GetEnumerator();
175188
}
176189

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

188200
public int Count => dictionary.Count;
189201

190-
public bool IsReadOnly => false;
191-
192202
public ICollection<TK> Keys => dictionary.Keys;
193203

194204
public ICollection<TV> Values => dictionary.Values;
@@ -211,6 +221,18 @@ public TV this[TK key]
211221
}
212222
}
213223
}
224+
225+
public bool IsReadOnly => false;
226+
227+
public static implicit operator Dictionary<TK, TV>(SerializedDictionary<TK, TV> serializedDictionary)
228+
{
229+
return serializedDictionary.dictionary;
230+
}
231+
232+
public static implicit operator SerializedDictionary<TK, TV>(Dictionary<TK, TV> dictionary)
233+
{
234+
return new SerializedDictionary<TK, TV>(dictionary);
235+
}
214236
}
215237
}
216238
#endif

Assets/Editor Toolbox/Runtime/Serialization/SerializedType.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public sealed class SerializedType : ISerializationCallbackReceiver
1515

1616
private Type type;
1717

18-
1918
/// <summary>
2019
/// Initializes a new instance of the <see cref="SerializedType"/> class.
2120
/// </summary>
@@ -45,7 +44,6 @@ public SerializedType(Type type)
4544
Type = type;
4645
}
4746

48-
4947
public static string GetReferenceValue(Type type)
5048
{
5149
return type != null
@@ -86,7 +84,6 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
8684
void ISerializationCallbackReceiver.OnBeforeSerialize()
8785
{ }
8886

89-
9087
/// <summary>
9188
/// Gets or sets type of class reference.
9289
/// </summary>
@@ -108,7 +105,6 @@ public Type Type
108105
}
109106
}
110107

111-
112108
public static implicit operator string(SerializedType typeReference) => typeReference.typeReference;
113109

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

0 commit comments

Comments
 (0)