-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializableComponent.cs
More file actions
146 lines (133 loc) · 7.22 KB
/
SerializableComponent.cs
File metadata and controls
146 lines (133 loc) · 7.22 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
public class SerializableComponent : XMLSerializableObject<SerializableComponent>
{
[XmlAttribute("ComponentName")]
public string componentName;
[XmlAttribute("ComponentId")]
public int componentId;
[XmlArray("ComponentProperties")]
[XmlArrayItem("ComponentProp")]
public List<SerializableComponentProperty> properties = new List<SerializableComponentProperty>();
public void ApplySerializedDataTo(Component comp)
{
foreach (var prop in properties)
{
Type type = comp.GetType();
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly;
PropertyInfo[] pinfos = type.GetProperties(flags);
foreach (var pinfo in pinfos) {
if (pinfo.CanWrite && pinfo.Name == prop.propertyName) {
try {
if (prop.propertyType == 0)
{
pinfo.SetValue(comp, prop.propertyValue, null);
}
else if (prop.propertyType == 1)
{
pinfo.SetValue(comp, int.Parse(prop.propertyValue), null);
}
else if (prop.propertyType == 2)
{
pinfo.SetValue(comp, float.Parse(prop.propertyValue), null);
}
else if (prop.propertyType == 3)
{
// Debug.Log("Trying to set " + copiedComponent.componentName + "." + prop.propertyName + " = " + prop.propertyValue +
// " [" + pinfo.PropertyType.ToString() + "] ");
pinfo.SetValue(comp, bool.Parse(prop.propertyValue), null);
}
else if (prop.propertyType == 4)
{
pinfo.SetValue(comp, SerializableVector3.LoadFromText(prop.propertyValue).GetVector3(), null);
}
else if (prop.propertyType == 5)
{
pinfo.SetValue(comp, SerializableVector2.LoadFromText(prop.propertyValue).GetVector2(), null);
}
else if (prop.propertyType == 6 && !string.IsNullOrEmpty(prop.propertyValue))
{
SerializableGameObjectReference sGObjectReference = SerializableGameObjectReference.LoadFromText(prop.propertyValue);
if (sGObjectReference != null)
{
ComponentAsText.gameObjectsDeserializableReferences.Add(sGObjectReference);
ComponentAsText.componentsToSetGameObject.Add(comp);
ComponentAsText.gameObjectPropertyNames.Add(prop.propertyName);
}
}
else if (prop.propertyType == 7 && !string.IsNullOrEmpty(prop.propertyValue))
{
SerializableComponentReference sComponentReference = SerializableComponentReference.LoadFromText(prop.propertyValue);
if (sComponentReference != null)
{
ComponentAsText.componentsDeserializableReferences.Add(sComponentReference);
ComponentAsText.componentsToSetComponent.Add(comp);
ComponentAsText.componentPropertyNames.Add(prop.propertyName);
}
}
}
catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific.
}
}
FieldInfo[] finfos = type.GetFields(flags);
foreach (var finfo in finfos) {
if (finfo.Name == prop.propertyName) {
try {
if (prop.propertyType == 0)
{
finfo.SetValue(comp, prop.propertyValue);
}
else if (prop.propertyType == 1)
{
finfo.SetValue(comp, int.Parse(prop.propertyValue));
}
else if (prop.propertyType == 2)
{
finfo.SetValue(comp, float.Parse(prop.propertyValue));
}
else if (prop.propertyType == 3)
{
// Debug.Log("Trying to set " + copiedComponent.componentName + "." + prop.propertyName + " = " + prop.propertyValue +
// " [" + finfo.FieldType.ToString() + "] ");
finfo.SetValue(comp, bool.Parse(prop.propertyValue));
}
else if (prop.propertyType == 4)
{
finfo.SetValue(comp, SerializableVector3.LoadFromText(prop.propertyValue).GetVector3());
}
else if (prop.propertyType == 5)
{
finfo.SetValue(comp, SerializableVector2.LoadFromText(prop.propertyValue).GetVector2());
}
else if (prop.propertyType == 6)
{
SerializableGameObjectReference sGObjectReference = SerializableGameObjectReference.LoadFromText(prop.propertyValue);
if (sGObjectReference != null)
{
ComponentAsText.gameObjectsDeserializableReferences.Add(sGObjectReference);
ComponentAsText.componentsToSetGameObject.Add(comp);
ComponentAsText.gameObjectPropertyNames.Add(prop.propertyName);
}
}
else if (prop.propertyType == 7)
{
SerializableComponentReference sComponentReference = SerializableComponentReference.LoadFromText(prop.propertyValue);
if (sComponentReference != null)
{
ComponentAsText.componentsDeserializableReferences.Add(sComponentReference);
ComponentAsText.componentsToSetComponent.Add(comp);
ComponentAsText.componentPropertyNames.Add(prop.propertyName);
}
}
}
catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific.
}
}
}
}
}