Skip to content

Commit 431ba99

Browse files
committed
readme
1 parent e8b0b16 commit 431ba99

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

Assets/Sample/MyMonoBehavior.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using GenericSerializeReference;
2+
using UnityEngine;
3+
4+
public interface IMyInterface<T> {}
5+
public class MyIntObject : IMyInterface<int> {}
6+
public struct StructWillNotShow : IMyInterface<int> {}
7+
public class MyMonoBehavior : MonoBehaviour
8+
{
9+
[GenericSerializeReference]
10+
public IMyInterface<int> Value { get; set; }
11+
}

Assets/Sample/MyMonoBehavior.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Sample/Test.unity

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ GameObject:
133133
m_Component:
134134
- component: {fileID: 366887100}
135135
- component: {fileID: 366887099}
136+
- component: {fileID: 366887101}
136137
m_Layer: 0
137138
m_Name: GameObject
138139
m_TagString: Untagged
@@ -220,3 +221,21 @@ Transform:
220221
m_Father: {fileID: 0}
221222
m_RootOrder: 0
222223
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
224+
--- !u!114 &366887101
225+
MonoBehaviour:
226+
m_ObjectHideFlags: 0
227+
m_CorrespondingSourceObject: {fileID: 0}
228+
m_PrefabInstance: {fileID: 0}
229+
m_PrefabAsset: {fileID: 0}
230+
m_GameObject: {fileID: 366887098}
231+
m_Enabled: 1
232+
m_EditorHideFlags: 0
233+
m_Script: {fileID: 11500000, guid: 810f573826344204a49b76ad339a67e3, type: 3}
234+
m_Name:
235+
m_EditorClassIdentifier:
236+
_Value:
237+
id: 0
238+
references:
239+
version: 1
240+
00000000:
241+
type: {class: , ns: , asm: }

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
1-
# GenericSerializeReference
2-
Make a field with generic type serializable by mark with [GenericSerializeReference] attribute.
1+
```c#
2+
public interface IMyInterface<T> {}
3+
public class MyIntObject : IMyInterface<int> {}
4+
5+
public class MyMonoBehavior : MonoBehaviour
6+
{
7+
[GenericSerializeReference]
8+
public IMyInterface<int> Value { get; set; }
9+
}
10+
```
11+
![image](https://user-images.githubusercontent.com/683655/111064372-b47b6280-84ee-11eb-90c2-22cfbdc65cc0.png)
12+
13+
# Limitations
14+
- Only types from referenced assemblies could be show up in inspector.
15+
- Not support `struct` type
16+
17+
# Costs
18+
- Extra time to generate IL instructions while building assembly
19+
- Extra memory space to store a generated field for each property.
20+
21+
# How it works
22+
```c#
23+
public class MyMonoBehavior : MonoBehaviour
24+
{
25+
// [GenericSerializeReference]
26+
// public IMyInterface<int> Value { get; set; }
27+
28+
// 1. gather derived types of property (`IMyInterface<>`)
29+
// then generate a concrete version of those types and make them all implement `IBase` interface
30+
private static class <Value>__generic_serialize_reference
31+
{
32+
public interface IBase {}
33+
public class MyIntObject : global::MyIntObject, IBase {}
34+
}
35+
36+
// 2. create a field named _Value with `IBase` type
37+
// which should be able to serialized by `SerializeReference` attribute
38+
[SerializeReference, GenericSerializeReferenceGeneratedField]
39+
private <Value>__generic_serialize_reference.IBase _Value;
40+
41+
// 3. inject code into property's getter and setter
42+
// make sure property get value from serialized field first
43+
// and set serialized field into null to avoid get from it next time.
44+
[GenericSerializeReference]
45+
public IMyInterface<int> Value
46+
{
47+
get
48+
{
49+
return (IMyInterface<int>) _Value ?? <Value>k__backingField;
50+
}
51+
set
52+
{
53+
<Value>k__backingField = value;
54+
_Value = null;
55+
}
56+
}
57+
}
58+
```

0 commit comments

Comments
 (0)