-
Notifications
You must be signed in to change notification settings - Fork 8
/
PolymorphismSerializationWindow.cs
120 lines (103 loc) · 2.74 KB
/
PolymorphismSerializationWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using SerializationTest.Common;
namespace SerializationTest.Test6
{
[Serializable]
public class Zoo : ISerializationCallbackReceiver
{
[NonSerialized]
public List<Animal> animals = new List<Animal>();
[SerializeField]
private List<Cat> catList;
[SerializeField]
private List<Dog> dogList;
[SerializeField]
private List<Giraffe> giraffeList;
[SerializeField]
private List<int> indexList;
public void OnBeforeSerialize() {
catList = new List<Cat>();
dogList = new List<Dog>();
giraffeList = new List<Giraffe>();
indexList = new List<int>();
for(int i = 0; i < animals.Count; i++) {
var type = animals[i].GetType();
if(type == typeof(Cat)) {
indexList.Add(0);
indexList.Add(catList.Count);
catList.Add((Cat)animals[i]);
}
if(type == typeof(Dog)) {
indexList.Add(1);
indexList.Add(dogList.Count);
dogList.Add((Dog)animals[i]);
}
if(type == typeof(Giraffe)) {
indexList.Add(2);
indexList.Add(giraffeList.Count);
giraffeList.Add((Giraffe)animals[i]);
}
}
}
public void OnAfterDeserialize() {
animals.Clear();
for(int i = 0; i < indexList.Count; i += 2) {
switch(indexList[i]) {
case 0:
animals.Add(catList[indexList[i + 1]]);
break;
case 1:
animals.Add(dogList[indexList[i + 1]]);
break;
case 2:
animals.Add(giraffeList[indexList[i + 1]]);
break;
}
}
indexList = null;
catList = null;
dogList = null;
giraffeList = null;
}
}
public class PolymorphismSerializationWindow : EditorWindow {
private const string WINDOW_TITLE = "Polymophism Serialization";
private Zoo zoo;
void OnEnable() {
titleContent.text = WINDOW_TITLE;
if(zoo == null) {
zoo = new Zoo();
}
}
void OnGUI() {
EditorGUILayout.LabelField("animal count", zoo.animals.Count.ToString());
for(int i = 0; i < zoo.animals.Count; i++) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(i.ToString(), zoo.animals[i].Species);
if(GUILayout.Button("remove", GUILayout.Width(70))) {
zoo.animals.RemoveAt(i);
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("add Cat")) {
zoo.animals.Add(new Cat());
}
if(GUILayout.Button("add Dog")) {
zoo.animals.Add(new Dog());
}
if(GUILayout.Button("add Giraffe")) {
zoo.animals.Add(new Giraffe());
}
EditorGUILayout.EndHorizontal();
}
[MenuItem ("Window/Serialization Test/Test 6 - " + WINDOW_TITLE)]
public static void ShowWindow()
{
EditorWindow.GetWindow<PolymorphismSerializationWindow>();
}
} // class TestWindow6
} // namespace SerializationTest.Test6