Skip to content

quabug/AnySerialize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openupm

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; }
}
Type Inspector
T[]
T[][]
T[][][]
image
List<T>
IList<T>
IReadOnlyList<T>
image
Dictionary<TKey, TValue>
IDictionary<TKey, TValue>
IReadOnlyDictionary<TKey, TValue>
image

T?

image

Lazy<T>

image

Guid

image

TimeSpan

image

DateTime

image
[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;
}
image

More Samples

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);
}

About

A complete serializer for Unity3D based on Unity3D serializer - `T[][]` `Dictionary` `record` `T?` `Lazy` `Guid` `TimeSpan` `DateTime` or your own type.

Resources

License

Stars

Watchers

Forks

Languages