-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentAsText.cs
677 lines (569 loc) · 26.4 KB
/
ComponentAsText.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System;
using System.Reflection;
public class SerializableGameObjectReference : XMLSerializableObject<SerializableGameObjectReference>
{
[XmlAttribute("GameObjectReferenceId")]
public int gameObjectReferenceId;
[XmlAttribute("GameObjectReferenceName")]
public string gameObjectReferenceName;
}
public class SerializableComponentReference : XMLSerializableObject<SerializableComponentReference>
{
[XmlAttribute("ComponentReferenceId")]
public int componentReferenceId;
[XmlAttribute("ComponentReferenceName")]
public string componentReferenceName;
}
public class SerializableVector3 : XMLSerializableObject<SerializableVector3>
{
[XmlAttribute("vecX")]
public float x;
[XmlAttribute("vecY")]
public float y;
[XmlAttribute("vecZ")]
public float z;
public SerializableVector3() {}
public SerializableVector3(Vector3 vector) { x = vector.x; y = vector.y; z = vector.z;}
public Vector3 GetVector3() { return new Vector3(x,y,z); }
}
public class SerializableVector2 : XMLSerializableObject<SerializableVector2>
{
[XmlAttribute("vecX")]
public float x;
[XmlAttribute("vecY")]
public float y;
public SerializableVector2() {}
public SerializableVector2(Vector2 vector) { x = vector.x; y = vector.y;}
public Vector2 GetVector2() { return new Vector2(x,y); }
}
public class SerializableComponentProperty : XMLSerializableObject<SerializableComponentProperty>
{
[XmlAttribute("PropertyName")]
public string propertyName;
[XmlAttribute("PropertyValue")]
public string propertyValue;
[XmlAttribute("PropertyType")]
public int propertyType;
}
public class SerializableGameObject : XMLSerializableObject<SerializableGameObject>
{
[XmlAttribute("GameObjectName")]
public string name;
[XmlAttribute("GameObjectId")]
public int gameObjectId;
[XmlArray("GameObjectComponents")]
[XmlArrayItem("Component")]
public List<SerializableComponent> components = new List<SerializableComponent>();
[XmlArray("SerializableChildren")]
[XmlArrayItem("SerializableChild")]
public List<SerializableGameObject> children = new List<SerializableGameObject>();
}
// <?xml version="1.0" encoding="utf-16"?>
// <SerializableGameObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <GameObjectComponents>
// <Component ComponentName="Transform" />
// <Component ComponentName="UIButton" />
// <Component ComponentName="VRInteractiveItem" />
// <Component ComponentName="NormalButton" />
// <Component ComponentName="TweenScale" />
// <Component ComponentName="SphereCollider" />
// <Component ComponentName="Rigidbody" />
// </GameObjectComponents>
// </SerializableGameObject>
public class ComponentAsText : EditorWindow
{
// In order to perform Serialization of GameObject references
public static List<SerializableGameObjectReference> gameObjectsSerializableReferences = new List<SerializableGameObjectReference>();
public static List<GameObject> referencedGameObjects = new List<GameObject>();
public static List<SerializableComponentProperty> objectReferenceProperties = new List<SerializableComponentProperty>();
// In order to perform Serialization of Component references
public static List<SerializableComponentReference> componentsSerializableReferences = new List<SerializableComponentReference>();
public static List<Component> referencedComponents = new List<Component>();
public static List<SerializableComponentProperty> componentReferenceProperties = new List<SerializableComponentProperty>();
// In order to perform Deserialization of GameObject references
public static List<SerializableGameObjectReference> gameObjectsDeserializableReferences = new List<SerializableGameObjectReference>();
public static List<Component> componentsToSetGameObject = new List<Component>();
public static List<string> gameObjectPropertyNames = new List<string>();
// In order to perform Deserialization of Component references
public static List<SerializableComponentReference> componentsDeserializableReferences = new List<SerializableComponentReference>();
public static List<Component> componentsToSetComponent= new List<Component>();
public static List<string> componentPropertyNames = new List<string>();
public static int currentGameObjectId = -1;
public static int currentComponentId = -1;
[MenuItem("GameObject/Tools/Copy as Text", false, 0)]
static void CopyGameObject()
{
Copy();
}
[MenuItem("GameObject/Tools/Paste from Text", false, 0)]
static void PasteGameObject()
{
Paste();
}
[MenuItem("CONTEXT/Transform/Copy as Text", false, 150)]
static void CopyTransform()
{
Transform target = Selection.activeTransform;
if (target != null)
{
SerializableComponent sComponent = GetSerializableComponent(target);
if (sComponent != null)
{
EditorGUIUtility.systemCopyBuffer = sComponent.GetString();
}
}
}
[MenuItem("CONTEXT/Transform/Paste Values from Text", false, 150)]
static void PasteTransform()
{
Transform target = Selection.activeTransform;
if (target != null)
{
SerializableComponent sComponent = SerializableComponent.LoadFromText(EditorGUIUtility.systemCopyBuffer);
if (sComponent != null)
{
sComponent.ApplySerializedDataTo(target);
}
}
}
[MenuItem("GameObject/Copy all components as Text")]
static void Copy()
{
EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 0f);
objectReferenceProperties.Clear();
gameObjectsSerializableReferences.Clear();
referencedGameObjects.Clear();
componentsSerializableReferences.Clear();
referencedComponents.Clear();
currentGameObjectId = 0;
currentComponentId = 0;
GameObject target = Selection.activeGameObject;
SerializableGameObject sGObject = GetSerializableGameObject(target);
EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 0.5f);
GetReferencesForSerialization(target, sGObject);
EditorUtility.DisplayProgressBar("Copying GameObjects", "...", 1f);
if (sGObject != null)
{
EditorGUIUtility.systemCopyBuffer = sGObject.GetString();
}
EditorUtility.ClearProgressBar();
}
static void VerifyReferencedGameObject(GameObject gameObject, SerializableGameObject sGObject)
{
for (int i = 0; i < referencedGameObjects.Count; i++)
{
if (gameObject == referencedGameObjects[i])
{
gameObjectsSerializableReferences[i].gameObjectReferenceId = sGObject.gameObjectId;
objectReferenceProperties[i].propertyValue = gameObjectsSerializableReferences[i].GetString();
}
}
}
static void VerifyReferencedComponents(Component component, SerializableComponent sComponent)
{
for (int i = 0; i < referencedComponents.Count; i++)
{
if (component == referencedComponents[i])
{
componentsSerializableReferences[i].componentReferenceId = sComponent.componentId;
componentReferenceProperties[i].propertyValue = componentsSerializableReferences[i].GetString();
}
}
}
static void GetReferencesForSerialization(GameObject gameObject, SerializableGameObject sGObject)
{
if (gameObject == null)
{
return;
}
VerifyReferencedGameObject(gameObject, sGObject);
Component[] copiedComponents = gameObject.GetComponents<Component>();
if (copiedComponents != null)
{
for (int c = 0; c < copiedComponents.Length; c++)
{
VerifyReferencedComponents(copiedComponents[c], sGObject.components[c]);
}
}
if (gameObject.transform.childCount > 0)
{
for (int s = 0; s < gameObject.transform.childCount; s++)
{
GetReferencesForSerialization(gameObject.transform.GetChild(s).gameObject, sGObject.children[s]);
}
}
}
static SerializableComponent GetSerializableComponent(Component component)
{
SerializableComponent sComponent = new SerializableComponent();
sComponent.componentId = currentComponentId;
sComponent.componentName = component.GetType ().Name;
currentComponentId++;
SerializableComponentProperty sProp;
Type type = component.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) {
// try {
// Debug.Log(sComponent.componentName + "." + pinfo.Name + " = " + pinfo.GetValue(copiedComponents[c], null).ToString() +
// " [" + pinfo.PropertyType.ToString() + "] ");
// }
// 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.
try {
if (pinfo.PropertyType == typeof(string) ||
pinfo.PropertyType == typeof(int) ||
pinfo.PropertyType == typeof(float) ||
pinfo.PropertyType == typeof(bool) )
{
sProp = new SerializableComponentProperty();
sProp.propertyName = pinfo.Name;
if (pinfo.PropertyType == typeof(string) && pinfo.GetValue(component, null) == null)
{
sProp.propertyValue = "";
}
else
{
sProp.propertyValue = pinfo.GetValue(component, null).ToString();
}
if (pinfo.PropertyType == typeof(string))
sProp.propertyType = 0;
else if (pinfo.PropertyType == typeof(int))
sProp.propertyType = 1;
else if (pinfo.PropertyType == typeof(float))
sProp.propertyType = 2;
else if (pinfo.PropertyType == typeof(bool))
sProp.propertyType = 3;
sComponent.properties.Add(sProp);
}
else if (pinfo.PropertyType == typeof(Vector3))
{
sProp = new SerializableComponentProperty();
sProp.propertyName = pinfo.Name;
sProp.propertyValue = new SerializableVector3((Vector3)pinfo.GetValue(component, null)).GetString();
sProp.propertyType = 4;
sComponent.properties.Add(sProp);
}
else if (pinfo.PropertyType == typeof(Vector2))
{
sProp = new SerializableComponentProperty();
sProp.propertyName = pinfo.Name;
sProp.propertyValue = new SerializableVector2((Vector2)pinfo.GetValue(component, null)).GetString();
sProp.propertyType = 5;
sComponent.properties.Add(sProp);
}
else if (pinfo.PropertyType == typeof(GameObject) && pinfo.GetValue(component, null) != null)
{
SerializableGameObjectReference sGObjectReference = new SerializableGameObjectReference()
{
gameObjectReferenceName = ((GameObject)pinfo.GetValue(component, null)).name
};
gameObjectsSerializableReferences.Add(sGObjectReference);
referencedGameObjects.Add((GameObject)pinfo.GetValue(component, null));
sProp = new SerializableComponentProperty();
sProp.propertyName = pinfo.Name;
//sProp.propertyValue
sProp.propertyType = 6;
sComponent.properties.Add(sProp);
objectReferenceProperties.Add(sProp);
}
else if ((pinfo.PropertyType == typeof(Component) || pinfo.PropertyType.IsSubclassOf(typeof(Component))) &&
pinfo.GetValue(component, null) != null)
{
SerializableComponentReference sComponentReference = new SerializableComponentReference()
{
componentReferenceName = ((Component)pinfo.GetValue(component, null)).GetType ().Name
};
componentsSerializableReferences.Add(sComponentReference);
referencedComponents.Add((Component)pinfo.GetValue(component, null));
sProp = new SerializableComponentProperty();
sProp.propertyName = pinfo.Name;
//sProp.propertyValue
sProp.propertyType = 7;
sComponent.properties.Add(sProp);
componentReferenceProperties.Add(sProp);
}
}
catch {}
}
}
FieldInfo[] finfos = type.GetFields(flags);
foreach (var finfo in finfos) {
if (finfo.FieldType == typeof(string) ||
finfo.FieldType == typeof(int) ||
finfo.FieldType == typeof(float) ||
finfo.FieldType == typeof(bool) )
{
sProp = new SerializableComponentProperty();
sProp.propertyName = finfo.Name;
if (finfo.FieldType == typeof(string) && finfo.GetValue(component) == null)
{
sProp.propertyValue = "";
}
else
{
sProp.propertyValue = finfo.GetValue(component).ToString();
}
if (finfo.FieldType == typeof(string))
sProp.propertyType = 0;
else if (finfo.FieldType == typeof(int))
sProp.propertyType = 1;
else if (finfo.FieldType == typeof(float))
sProp.propertyType = 2;
else if (finfo.FieldType == typeof(bool))
sProp.propertyType = 3;
sComponent.properties.Add(sProp);
}
else if (finfo.FieldType == typeof(Vector3))
{
sProp = new SerializableComponentProperty();
sProp.propertyName = finfo.Name;
sProp.propertyValue = new SerializableVector3((Vector3)finfo.GetValue(component)).GetString();
sProp.propertyType = 4;
sComponent.properties.Add(sProp);
}
else if (finfo.FieldType == typeof(Vector2))
{
sProp = new SerializableComponentProperty();
sProp.propertyName = finfo.Name;
sProp.propertyValue = new SerializableVector2((Vector2)finfo.GetValue(component)).GetString();
sProp.propertyType = 5;
sComponent.properties.Add(sProp);
}
else if (finfo.FieldType == typeof(GameObject) && ((GameObject)finfo.GetValue(component)) != null)
{
SerializableGameObjectReference sGObjectReference = new SerializableGameObjectReference()
{
gameObjectReferenceName = ((GameObject)finfo.GetValue(component)).name
};
gameObjectsSerializableReferences.Add(sGObjectReference);
referencedGameObjects.Add((GameObject)finfo.GetValue(component));
sProp = new SerializableComponentProperty();
sProp.propertyName = finfo.Name;
//sProp.propertyValue
sProp.propertyType = 6;
sComponent.properties.Add(sProp);
objectReferenceProperties.Add(sProp);
}
else if ((finfo.FieldType == typeof(Component) || finfo.FieldType.IsSubclassOf(typeof(Component))) &&
finfo.GetValue(component) != null)
{
SerializableComponentReference sComponentReference = new SerializableComponentReference()
{
componentReferenceName = ((Component)finfo.GetValue(component)).GetType ().Name
};
componentsSerializableReferences.Add(sComponentReference);
referencedComponents.Add((Component)finfo.GetValue(component));
sProp = new SerializableComponentProperty();
sProp.propertyName = finfo.Name;
//sProp.propertyValue
sProp.propertyType = 7;
sComponent.properties.Add(sProp);
componentReferenceProperties.Add(sProp);
}
}
return sComponent;
}
static SerializableGameObject GetSerializableGameObject(GameObject gameObject)
{
if (gameObject == null)
{
return null;
}
Component[] copiedComponents = gameObject.GetComponents<Component>();
if (copiedComponents != null)
{
SerializableGameObject sGObject = new SerializableGameObject();
sGObject.gameObjectId = currentGameObjectId;
sGObject.name = gameObject.name;
SerializableComponent sComponent;
currentGameObjectId++;
for (int c = 0; c < copiedComponents.Length; c++)
{
sComponent = GetSerializableComponent(copiedComponents[c]);
sGObject.components.Add(sComponent);
}
if (gameObject.transform.childCount > 0)
{
SerializableGameObject sChildrenGObject;
for (int s = 0; s < gameObject.transform.childCount; s++)
{
sChildrenGObject = GetSerializableGameObject(gameObject.transform.GetChild(s).gameObject);
sGObject.children.Add(sChildrenGObject);
}
}
return sGObject;
}
return null;
}
[MenuItem("GameObject/Paste all components from Text")]
static void Paste()
{
EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0f);
string componentsText = EditorGUIUtility.systemCopyBuffer;
//Debug.Log("gil - ComponentAsText::Paste - " + componentsText);
gameObjectsDeserializableReferences.Clear();
componentsToSetGameObject.Clear();
gameObjectPropertyNames.Clear();
componentsDeserializableReferences.Clear();
componentsToSetComponent.Clear();
componentPropertyNames.Clear();
SerializableGameObject sGObject = SerializableGameObject.LoadFromText(componentsText);
EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0.3f);
if (sGObject != null)
{
//Debug.Log("gil - ComponentAsText::Paste - " + Selection.gameObjects.Length);
foreach (var targetGameObject in Selection.gameObjects)
{
if (!targetGameObject) continue;
Debug.Log("gil - ComponentAsText::Paste - GameObject.name = " + targetGameObject.name);
PasteSerializableGameObject(targetGameObject, sGObject);
EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 0.6f);
GetReferencesForDeserialization(targetGameObject, sGObject);
EditorUtility.DisplayProgressBar("Paste GameObjects", "...", 1f);
}
}
EditorUtility.ClearProgressBar();
}
static void VerifyGameObject(GameObject gameObject, SerializableGameObject sGObject)
{
for (int i = 0; i < gameObjectsDeserializableReferences.Count; i++)
{
if (sGObject.gameObjectId == gameObjectsDeserializableReferences[i].gameObjectReferenceId)
{
Type type = componentsToSetGameObject[i].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 == gameObjectPropertyNames[i]) {
try {
pinfo.SetValue(componentsToSetGameObject[i], gameObject, null);
} catch {}
}
}
FieldInfo[] finfos = type.GetFields(flags);
foreach (var finfo in finfos) {
if (finfo.Name == gameObjectPropertyNames[i]) {
try {
finfo.SetValue(componentsToSetGameObject[i], gameObject);
} catch {}
}
}
}
}
}
/// <summary>
/// Assumption: component represents the same sComponent from source object
/// </summary>
static void VerifyComponent(Component component, SerializableComponent sComponent)
{
//Debug.Log("gil - ComponentAsText::VerifyComponent - testing " + component.GetType().Name + " with SerializableComponent " + sComponent.componentName);
for (int i = 0; i < componentsDeserializableReferences.Count; i++)
{
if (sComponent.componentId == componentsDeserializableReferences[i].componentReferenceId)
{
Type type = componentsToSetComponent[i].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 == componentPropertyNames[i]) {
try {
pinfo.SetValue(componentsToSetComponent[i], component, null);
} catch {}
}
}
FieldInfo[] finfos = type.GetFields(flags);
foreach (var finfo in finfos) {
if (finfo.Name == componentPropertyNames[i]) {
try {
finfo.SetValue(componentsToSetComponent[i], component);
} catch {}
}
}
}
}
}
static void GetReferencesForDeserialization(GameObject gameObject, SerializableGameObject sGObject)
{
if (gameObject == null)
{
return;
}
//Debug.Log("gil - ComponentAsText::GetReferencesForDeserialization - Testing " + gameObject.name + " with sGObject " + sGObject.name);
VerifyGameObject(gameObject, sGObject);
Component[] copiedComponents = gameObject.GetComponents<Component>();
if (copiedComponents != null)
{
List<SerializableComponent> sComponents = sGObject.components;
for (int c = 0; c < sComponents.Count; c++)
{
for (int i = 0; i < copiedComponents.Length; i++)
{
if (copiedComponents[i] != null && copiedComponents[i].GetType().Name == sComponents[c].componentName)
{
VerifyComponent(copiedComponents[i], sComponents[c]);
copiedComponents[i] = null;
break;
}
}
}
}
if (gameObject.transform.childCount > 0)
{
for (int s = 0; s < gameObject.transform.childCount; s++)
{
GetReferencesForDeserialization(gameObject.transform.GetChild(s).gameObject, sGObject.children[s]);
}
}
}
static bool isDuplicatedComponent(GameObject gameObject, SerializableGameObject sGObject, SerializableComponent component)
{
int numberOfComponents = 0;
foreach (var copiedComponent in sGObject.components)
{
if (copiedComponent.componentName == component.componentName)
{
numberOfComponents++;
}
}
return gameObject.GetComponent(component.componentName) != null && numberOfComponents == 1;
}
static void PasteSerializableGameObject(GameObject gameObject, SerializableGameObject sGObject)
{
gameObject.name = sGObject.name;
foreach (var copiedComponent in sGObject.components)
{
if (copiedComponent == null) continue;
Component comp;
if (copiedComponent.componentName == "Transform")
{
comp = (Component) gameObject.transform;
}
else if (isDuplicatedComponent(gameObject, sGObject, copiedComponent))
{
comp = gameObject.GetComponent(copiedComponent.componentName);
}
else
{
comp = UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Editor/Tools/CopyComponents/ComponentAsText.cs (134,21)", copiedComponent.componentName);
}
if (comp == null) continue;
copiedComponent.ApplySerializedDataTo(comp);
}
GameObject childGObject;
foreach (var sGChild in sGObject.children)
{
childGObject = new GameObject();
childGObject.transform.parent = gameObject.transform;
PasteSerializableGameObject(childGObject, sGChild);
}
}
}