@@ -20,7 +20,6 @@ Unity3D 2020.2+ (not guaranteed to work below 2020.2)
20
20
[ OpenUPM] ( https://openupm.com/packages/com.quabug.generic-serialize-reference/ ) : ` openupm add com.quabug.generic-serialize-reference `
21
21
22
22
## 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)
24
23
- Not support ` struct ` type.
25
24
- Not support generic field.
26
25
- Not support variance.
@@ -30,26 +29,64 @@ Unity3D 2020.2+ (not guaranteed to work below 2020.2)
30
29
- Extra memory space to store a generated field for each property.
31
30
32
31
## How it works
32
+
33
+ ### AssemblyCSharp Mode
33
34
``` c#
35
+ // Generate derived types into AssemblyCSharp.dll
34
36
public class MyMonoBehavior : MonoBehaviour
35
37
{
36
- // [GenericSerializeReference]
38
+ // [GenericSerializeReference(mode: GenerateMode.AssemblyCSharp) ]
37
39
// public IMyInterface<int> Value { get; set; }
38
40
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
42
51
{
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
+ }
45
61
}
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; }
46
83
47
- // 2 . create a field named _Value with `IBase` type
84
+ // 1 . create a field named _Value with `IBase` type
48
85
// which should be able to serialized by `SerializeReference` attribute
49
86
[SerializeReference , GenericSerializeReferenceGeneratedField ]
50
87
private <Value>__generic_serialize_reference.IBase _Value ;
51
88
52
- // 3 . inject code into property's getter and setter
89
+ // 2 . inject code into property's getter and setter
53
90
// make sure property get value from serialized field first
54
91
// and setter set serialized field into null to avoid get from it next time.
55
92
[GenericSerializeReference ]
@@ -65,6 +102,15 @@ public class MyMonoBehavior : MonoBehaviour
65
102
_Value = null ;
66
103
}
67
104
}
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
+
68
114
}
69
115
```
70
116
0 commit comments