Tanoda
pb_SceneNode.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.Serialization;
5using UnityEngine;
6
8{
16 [System.Serializable()]
17 public class pb_SceneNode : ISerializable
18 {
19 public string name;
20 public string hash;
21 public string rename;
23 public Vector3 localPos;
24 public object convex;
25 public object trigger;
27 public List<pb_SceneNode> children;
28
30 public List<pb_ISerializable> components;
31
32 private GameObject thisGO;
33
37 public pb_SceneNode() { } //Az alap JsonUtility használná ezt
38
42 public pb_SceneNode(SerializationInfo info, StreamingContext context)
43 {
44 name = (string)info.GetValue("name", typeof(string));
45 transform = (pb_Transform)info.GetValue("transform", typeof(pb_Transform));
46 children = (List<pb_SceneNode>)info.GetValue("children", typeof(List<pb_SceneNode>));
47 localPos = (Vector3)info.GetValue("localPos", typeof(Vector3));
48
49 foreach (var e in info)
50 {
51 try
52 {
53 if (e.Name == "cth")
54 {
55 var asd = e.Value.ToString();
56 changedTexture = Newtonsoft.Json.JsonConvert.DeserializeObject<ChangedTextureHolder>(asd);
57 //changedTexture = JsonUtility.FromJson<ChangedTextureHolder>(asd);
58 }
59 if (e.Name == "hash")
60 {
61 hash = e.Value.ToString();
62 }
63 if (e.Name == "rename")
64 {
65 rename = e.Value.ToString();
66 }
67 if (e.Name == "trigger")
68 {
69 trigger = Convert.ToBoolean(e.Value);
70 }
71 if (e.Name == "convex")
72 {
73 convex = Convert.ToBoolean(e.Value);
74 }
75 if (e.Name == "components")
76 {
77 var deserializeHack = e.Value.ToString();
78 var comps = Newtonsoft.Json.JsonConvert.DeserializeObject<List<pb_SerializableObject<Component>>>(deserializeHack);
79 var val = new List<pb_ISerializable>();
80 val.AddRange(comps);
81 //components = JsonUtility.FromJson<List<pb_ISerializable>>(deserializeHack);
82 components = val;
83
84 }
85
86 }
87 catch (Exception ex)
88 {
89
90 }
91 }
92 }
93
97 public void GetObjectData(SerializationInfo info, StreamingContext context)
98 {
99 info.AddValue("name", name, typeof(string));
100 try
101 {
102 info.AddValue("hash", thisGO.GetComponent<HashHolder>().Hash, typeof(string));
103 }
104 catch (Exception)
105 {
106 // ignored
107 }
108 try
109 {
110 var positioner = thisGO.GetComponent<Positioner>();
111 if (positioner)
112 {
113 info.AddValue("stage", positioner.ActiveInStage, typeof(int));
114 info.AddValue("finalpos", positioner.FinalPosition, typeof(Vector3));
115 info.AddValue("finalrot", positioner.FinalRotation, typeof(Vector3));
116 info.AddValue("finalfwd", positioner.FinalForward, typeof(Vector3));
117 info.AddValue("finalrgt", positioner.FinalRight, typeof(Vector3));
118 info.AddValue("final_up", positioner.FinalUp, typeof(Vector3));
119 info.AddValue("tolp", positioner.TolerancePos, typeof(float));
120 info.AddValue("tolr", positioner.ToleranceRot, typeof(float));
121 }
122 }
123 catch (Exception)
124 {
125 // ignored
126 }
127 try
128 {
129 var tholder = thisGO.GetComponent<TargetHolder>();
130 if (tholder)
131 {
132 info.AddValue("holderIndex", tholder.targetIndex, typeof(int));
133 }
134 }
135 catch (Exception)
136 {
137 // ignored
138 }
139 info.AddValue("transform", transform, typeof(pb_Transform));
140 info.AddValue("localPos", thisGO.transform.localPosition, typeof(Vector3));
141 try
142 {
143 var coll = thisGO.GetComponentInChildren<MeshCollider>();
144 if (coll)
145 {
146 info.AddValue("convex", coll.convex, typeof(bool));
147 info.AddValue("trigger", coll.isTrigger, typeof(bool));
148 }
149 }
150 catch (Exception)
151 {
152 // ignored
153 }
154
155 try
156 {
157 var rn = thisGO.GetComponent<RenameObject>();
158 if (rn && !string.IsNullOrEmpty(rn.NewName))
159 {
160 info.AddValue("rename", rn.NewName, typeof(string));
161 }
162 }
163 catch (Exception)
164 {
165 // ignored
166 }
167
168 try
169 {
170 var mr = thisGO.GetComponent<MeshRenderer>();
171 if (mr)
172 {
173 var _materials = mr.materials;
174
175 if (_materials != null)
176 {
177 int i = 0;
178 foreach (var _material in _materials)
179 {
180 //if (_material.HasProperty("_Metallic"))
181 //{
182 // info.AddValue("metallic", _material.GetFloat("_Metallic"), typeof(float));
183 //}
184
185 //if (_material.HasProperty("_SmoothnessTextureChannel"))
186 //{
187 // info.AddValue("smoothness", _material.GetFloat("_SmoothnessTextureChannel"), typeof(float));
188 //}
189
190
191 //if (_material.HasProperty("_Color"))
192 //{
193 // info.AddValue("color", _material.GetColor("_Color"), typeof(Color));
194 //}
195 var cth = thisGO.GetComponent<ChangedTextureHolder>();
196 if (cth)
197 info.AddValue("cth", cth, typeof(ChangedTextureHolder));
198 //var cth = thisGO.GetComponent<ChangedTextureHolder>();
199 //if (cth)
200 //{
201 // if (_material.HasProperty("_MainTex"))
202 // {
203 // info.AddValue("texture", cth.textureName, typeof(string));
204 // }
205 // if (_material.HasProperty("_MetallicGlossMap"))
206 // {
207 // info.AddValue("metallicmap", cth.textureName, typeof(string));
208 // }
209 // if (_material.HasProperty("_BumpMap"))
210 // {
211 // info.AddValue("normal", cth.textureName, typeof(string));
212 // }
213 // if (_material.HasProperty("_OcclusionMap"))
214 // {
215 // info.AddValue("occlusion", cth.textureName, typeof(string));
216 // }
217 //
218 //}
219 i++;
220 }
221 }
222 }
223 }
224 catch (Exception)
225 {
226 // ignored
227 }
228
229 info.AddValue("children", children, typeof(List<pb_SceneNode>));
230 //info.AddValue("metadata", metadata, typeof(pb_MetaData));
231
232 //if( metadata == null || metadata.assetType == AssetType.Instance )
233 info.AddValue("components", components, typeof(List<pb_SerializableObject<Component>>));
234 }
235
239 public pb_SceneNode(GameObject root)
240 {
241 thisGO = root;
242 name = root.name;
243 hash = root.GetComponent<HashHolder>() ? root.GetComponent<HashHolder>().Hash : root.AddComponent<HashHolder>().Hash;
244 components = new List<pb_ISerializable>();
245
246 pb_MetaDataComponent metadata_component = root.GetComponent<pb_MetaDataComponent>();
247
248 if (metadata_component == null)
249 metadata_component = root.AddComponent<pb_MetaDataComponent>();
250
251 metadata = metadata_component.metadata;
252
253 if (metadata.assetType == AssetType.Instance && false)
254 {
255 foreach (Component c in root.GetComponents<Component>())
256 {
257 if (c == null ||
258 c is Transform ||
259 c.GetType().GetCustomAttributes(true).Any(x => x is pb_JsonIgnoreAttribute))
260 continue;
261
262 components.Add(pb_Serialization.CreateSerializableObject<Component>(c));
263 }
264 }
265 else if (root.GetComponent<MeshRenderer>())
266 {
267 components.Add(pb_Serialization.CreateSerializableObject<Component>(root.GetComponent<MeshRenderer>()));
268 }
269
270 // avoid calling constructor which automatically rebuilds the matrix
271 transform = new pb_Transform();
272 transform.SetTRS(root.transform);
273 transform.isStatic = root.GetComponent<StaticGO>();
274 transform.isTool = root.GetComponent<ToolGO>();
275 transform.isRestArea = root.GetComponent<RestArea>();
276 //transform.isTwoHandGrab = root.GetComponent<TwoHandGrab>();
277 try
278 {
279 transform.offset = root.GetComponent<OffsetHolder>().Offset;
280 }
281 catch (Exception)
282 {
283 // ignored
284 }
285 try
286 {
287 transform.rotoffset = root.GetComponent<OffsetHolder>().Rotation;
288 }
289 catch (Exception)
290 {
291 // ignored
292 }
293 try
294 {
295 transform.leftoffset = root.GetComponent<OffsetHolder>().LeftOffset;
296 }
297 catch (Exception)
298 {
299 // ignored
300 }
301 try
302 {
303 transform.leftrotoffset = root.GetComponent<OffsetHolder>().LeftRotation;
304 }
305 catch (Exception)
306 {
307 // ignored
308 }
309
310 children = new List<pb_SceneNode>();
311
312 foreach (Transform t in root.transform)
313 {
314 //if(!t.gameObject.activeSelf)
315 // continue;
316
317 children.Add(new pb_SceneNode(t.gameObject));
318 }
319 }
320
324 public GameObject ToGameObject()
325 {
326 return thisGO;
327 GameObject go;
328
329 if (metadata.assetType == AssetType.Instance)
330 {
331 go = new GameObject();
332
333 foreach (pb_ISerializable serializedObject in components)
334 go.AddComponent(serializedObject);
335 }
336 else
337 {
339
340 if (prefab != null)
341 {
342 go = GameObject.Instantiate(prefab);
343 metadata.componentDiff.ApplyPatch(go);
344 }
345 else
346 {
347 go = new GameObject();
348 }
349 }
350
351 pb_MetaDataComponent mc = go.DemandComponent<pb_MetaDataComponent>();
352 mc.metadata = metadata;
353
354 go.name = name;
355 go.transform.SetTRS(transform);
356
357 foreach (pb_SceneNode childNode in children)
358 {
359 GameObject child = childNode.ToGameObject();
360 child.transform.SetParent(go.transform);
361 }
362
363 return go;
364 }
365
366 public static explicit operator GameObject(pb_SceneNode node)
367 {
368 return node.ToGameObject();
369 }
370 }
371}
UnityEngine.Component Component
void GetObjectData(SerializationInfo info, StreamingContext context)
Definition: pb_SceneNode.cs:97
ChangedTextureHolder changedTexture
Definition: pb_SceneNode.cs:26
List< pb_SceneNode > children
Definition: pb_SceneNode.cs:27
List< pb_ISerializable > components
Definition: pb_SceneNode.cs:30
pb_SceneNode(SerializationInfo info, StreamingContext context)
Definition: pb_SceneNode.cs:42
pb_ComponentDiff componentDiff
Definition: pb_MetaData.cs:26
AssetType assetType
Definition: pb_MetaData.cs:83
static GameObject LoadPrefabWithMetadata(pb_MetaData metadata)
void SetTRS(Transform trs)
string Hash
Definition: HashHolder.cs:18
Definition: ToolGO.cs:6
AssetType
Definition: pb_Enum.cs:69