Skip to content

Commit c0ffc1c

Browse files
authored
Update README.md
1 parent 525b182 commit c0ffc1c

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

README.md

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Unity3D 2020.2+ (not guaranteed to work below 2020.2)
2020
[OpenUPM](https://openupm.com/packages/com.quabug.generic-serialize-reference/): `openupm add com.quabug.generic-serialize-reference`
2121

2222
## Limitations
23-
- Only types from referenced assemblies could be show up in inspector. (usually this is not a big deal when writing game code, but become a major drawback when writing a library)
2423
- Not support `struct` type.
2524
- Not support generic field.
2625
- Not support variance.
@@ -30,26 +29,64 @@ Unity3D 2020.2+ (not guaranteed to work below 2020.2)
3029
- Extra memory space to store a generated field for each property.
3130

3231
## How it works
32+
33+
### AssemblyCSharp Mode
3334
```c#
35+
// Generate derived types into AssemblyCSharp.dll
3436
public class MyMonoBehavior : MonoBehaviour
3537
{
36-
// [GenericSerializeReference]
38+
// [GenericSerializeReference(mode: GenerateMode.AssemblyCSharp)]
3739
// public IMyInterface<int> Value { get; set; }
3840
39-
// 1. gather derived types of property (`IMyInterface<>`)
40-
// then generate a non-generic version of those types and make them all implement `IBase` interface
41-
private static class <Value>__generic_serialize_reference
41+
// 1. create a field named _Value with `IBase` type
42+
// which should be able to serialized by `SerializeReference` attribute
43+
[SerializeReference, GenericSerializeReferenceGeneratedField]
44+
private GenericSerializeReference.IBase _Value;
45+
46+
// 2. inject code into property's getter and setter
47+
// make sure property get value from serialized field first
48+
// and setter set serialized field into null to avoid get from it next time.
49+
[GenericSerializeReference]
50+
public IMyInterface<int> Value
4251
{
43-
public interface IBase {}
44-
public class MyIntObject : global::MyIntObject, IBase {}
52+
get
53+
{
54+
return (IMyInterface<int>) _Value ?? <Value>k__backingField;
55+
}
56+
set
57+
{
58+
<Value>k__backingField = value;
59+
_Value = null;
60+
}
4561
}
62+
}
63+
64+
// AssemblyCSharp.dll
65+
// 3. gather derived types of property (`IMyInterface<int>`)
66+
// then generate a non-generic version of those types and make them all implement `IBase` interface
67+
namespace <GenericSerializeReference>
68+
{
69+
static class IMyInterface`1<System_Int32>
70+
{
71+
class MyIntObject : global::MyIntObject, GenericSerializeReference.IBase {}
72+
}
73+
}
74+
```
75+
76+
### Embed Mode
77+
```c#
78+
// Embed into current class
79+
public class MyMonoBehavior : MonoBehaviour
80+
{
81+
// [GenericSerializeReference(mode: GenerateMode.Embed)]
82+
// public IMyInterface<int> Value { get; set; }
4683
47-
// 2. create a field named _Value with `IBase` type
84+
// 1. create a field named _Value with `IBase` type
4885
// which should be able to serialized by `SerializeReference` attribute
4986
[SerializeReference, GenericSerializeReferenceGeneratedField]
5087
private <Value>__generic_serialize_reference.IBase _Value;
5188

52-
// 3. inject code into property's getter and setter
89+
// 2. inject code into property's getter and setter
5390
// make sure property get value from serialized field first
5491
// and setter set serialized field into null to avoid get from it next time.
5592
[GenericSerializeReference]
@@ -65,6 +102,15 @@ public class MyMonoBehavior : MonoBehaviour
65102
_Value = null;
66103
}
67104
}
105+
106+
// 3. gather derived types of property (`IMyInterface<int>`)
107+
// then generate a non-generic version of those types and make them all implement `IBase` interface
108+
private static class <Value>__generic_serialize_reference
109+
{
110+
public interface IBase {}
111+
public class MyIntObject : global::MyIntObject, IBase {}
112+
}
113+
68114
}
69115
```
70116

0 commit comments

Comments
 (0)