-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializebleCollection.cs
80 lines (70 loc) · 1.93 KB
/
SerializebleCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[Serializable]
public class SerializebleDictionary<Key, Value> : Dictionary<Key, Value>, ISerializationCallbackReceiver
{
[Serializable]
public struct SerializableKeyValuePair<TKey, TValue>
{
public SerializableKeyValuePair(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
public TKey key;
public TValue value;
}
[SerializeField] List<SerializableKeyValuePair<Key, Value>> serialize;
public void OnBeforeSerialize()
{
serialize = this.Select(pair => new SerializableKeyValuePair<Key, Value>(pair.Key, pair.Value)).ToList();
}
public void OnAfterDeserialize()
{
Clear();
foreach (var keyValuePair in serialize)
Add(keyValuePair.key, keyValuePair.value);
}
}
[Serializable]
public class SerializebleHashSet<T> : HashSet<T>, ISerializationCallbackReceiver
{
[SerializeField] List<T> serialize;
public void OnBeforeSerialize()
{
serialize = new List<T>(this);
}
public void OnAfterDeserialize()
{
Clear();
foreach (var value in serialize)
Add(value);
}
}
[Serializable]
public struct SerializebleNullable<T> where T : struct
{
[SerializeField] T value;
[SerializeField] bool hasValue;
public T Value
{
get
{
if (HasValue == false)
return default;
return value;
}
}
public bool HasValue => hasValue;
public SerializebleNullable(T value)
{
this.value = value;
hasValue = true;
}
public static implicit operator SerializebleNullable<T>(T value) => new(value);
public static implicit operator SerializebleNullable<T>(T? value) => value.HasValue ? new SerializebleNullable<T>(value.Value) : new SerializebleNullable<T>();
public static implicit operator T?(SerializebleNullable<T> value) => value.HasValue ? value.Value : null;
public override string ToString() => hasValue ? value.ToString() : string.Empty;
}