Awesome
AnySerialize for Unity3D
A complete serializer for Unity3D based on Unity3D serializer.
T[][]
Dictionary
record
T?
Lazy
Guid
TimeSpan
DateTime
or your own type.
Installation
- OpenUPM: follow the instrustions on OpenUPM
- or Unity Package: download packages of both AnyProcessor and AnySerialize in release page and import them into Unity project.
Usage
Use AnySerializeAttribute
for any property need to be serialized and edit in inspector.
public class MyComponent : MonoBehaviour
{
[AnySerialize] public Dictionary<int, Lazy<Dictionary<Guid, TimeSpan>>> Value { get; }
}
<table>
<tr>
<td align="center"><strong>Type</strong></td> <td align="center"><strong>Inspector</strong></td>
</tr>
<tr>
<td>
T[]
T[][]
T[][][]
</td>
<td><img width="464" alt="image" src="https://user-images.githubusercontent.com/683655/190845247-f41d2334-a085-43a2-bd64-886d5637c931.png"></td>
</tr>
<tr>
<td>
List<T>
IList<T>
IReadOnlyList<T>
</td>
<td><img width="468" alt="image" src="https://user-images.githubusercontent.com/683655/190845505-794e6fd1-e4d3-4a5e-91b3-ca003cb5f22b.png"></td>
</tr>
<tr>
<td>
Dictionary<TKey, TValue>
IDictionary<TKey, TValue>
IReadOnlyDictionary<TKey, TValue>
</td>
<td><img width="484" alt="image" src="https://user-images.githubusercontent.com/683655/190845565-44e05a1b-ea41-43c6-a391-11c07f465f63.png"></td>
</tr>
<tr>
<td>
T?
Lazy<T>
Guid
TimeSpan
DateTime
[AnySerializable]
public class AnySerializableClass
{
// optional, but recommend.
// prevent issue on reorder fields.
[AnySerializeFieldOrder(0)]
public int[][] Array2;
[AnySerializeFieldOrder(1)]
public Dictionary<int, string> Dict;
[AnySerializeFieldOrder(2)]
public int? Nullable;
[AnySerializeFieldOrder(3)]
public Lazy<int> Lazy;
}
</td>
<td><img width="485" alt="image" src="https://user-images.githubusercontent.com/683655/190846815-070ac215-325d-49f2-a813-70dee839013a.png"></td>
</tr>
</table>
Custom Serializable Types
// custom serializable value type
[Serializable]
public class AnyGuid :
IReadOnlyAny<Guid>, // IReadOnlyAny<T> for readonly property (get only)
IAny<Guid> // IAny<T> for read-write property (get and set)
{
[SerializeField] private string _guid; // unity serialize field
public Guid Value
{
get => Guid.Parse(_guid); // convert serialize field to output value
set => _guid = value.ToString(); // convert output value to serialize field
}
}
// custom serializable container type
[Serializable]
// use AnyConstraintTypeAttribute on generic parameter to automatically find type by its constraint
// e.g. T is int, then TAny will be replaced by AnyValue_Int32
public class ReadOnlyAnyList<T, [AnyConstraintType] TAny>
: IReadOnlyAny<List<T>>
where TAny : IReadOnlyAny<T>
{
[SerializeField] private List<TAny> _value = default!;
public List<T> Value => _value.Select(v => v.Value);
}