-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceWithPrefab.cs
829 lines (747 loc) · 33.4 KB
/
ReplaceWithPrefab.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*=================== Replace with Prefab ===================
Unity Forum Community Thread https://forum.unity.com/threads/replace-game-object-with-prefab.24311/
Tested in 2018.4.19f1, 2019.3.6f1, 2020.1.12f1
Should work in pre-2018.3 versions with old prefab workflow (Needs testing)
Changelog and contributors:
v1.0.0 (2010-03) Original CopyComponents by Michael L. Croswell for Colorado Game Coders, LLC
v1.1.0 (2011-06) by Kristian Helle Jespersen
v1.2.0 (2015-04) by Connor Cadellin McKee for Excamedia
v1.3.0 (2015-04) by Fernando Medina (fermmmm)
v1.4.0 (2015-07) by Julien Tonsuso (www.julientonsuso.com)
v1.5.0 (2017-06) by Alex Dovgodko
Changed into editor window and added instant preview in scene view
v1.6.0 (2018-03) by ???
Made changes to make things work with Unity 5.6.1
v1.7.0 (2018-05) by Carlos Diosdado (hypertectonic)
Added link to community thread, booleans to choose if scale and rotation are applied, mark scene as dirty, changed menu item
v1.8.0 (2018-??) by Virgil Iordan
Added KeepPlaceInHierarchy
v1.9.0 (2019-01) by Dev Bye-A-Jee, Sanjay Sen & Nick Rodriguez for Ravensbourne University London
Added unique numbering identifier in the hierarchy to each newly instantiated prefab, also accounts for existing numbers
v2.0.0 (2020-03-22) by Zan Kievit
Made compatible with the new Prefab system of Unity 2018. Made more user friendly and added undo.
v2.1.0 (2020-03-22) by Carlos Diosdado (hypertectonic)
Added options to use as a utility window (show from right click in the hierarchy), min/max window size,
backwards compatibility for old prefab system, works with prefabs selected in project browser, fixed not replacing prefabs,
added version numbers, basic documentation, Community namespace to avoid conflicts, cleaned up code for readability.
v2.2.0 (2020-03-22) by GGHitman
Add Search by tag or by layer
the object will replace the tag and the layer of the original object
compare and exchange materials
v.2.3.0 (2020-10-20) by Jade Annand
Added recursive game object, component, field and value copying.
v.2.4.0 (2020-11-9) by Zan Kievit
Fixed Rename errors. Added Numbering Schemes from project settings to Rename, dynamic preview of Rename, improved overal UX.
Added NaturalComparer class from https://www.codeproject.com/Articles/22517/Natural-Sort-Comparer for human readable sorting.
Known Errors: None.
To Do: Replace with any objects, besides only Prefabs as of now.
============================================================*/
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
namespace Community
{
/// <summary>
/// An editor tool to replace selected GameObjects with a specified Prefab.
/// </summary>
public class ReplaceWithPrefab : EditorWindow
{
public GameObject prefab = null;
public GameObject oldPrefab = null;
public List<GameObject> objectsToReplace = new List<GameObject>();
public List<GameObject> newObjects = new List<GameObject>();
public List<string> objectPreview = new List<string>();
public bool editMode = false;
public struct ReplacementPreferences
{
public bool renameObjects;
public bool orderHierarchyToPreview;
public bool applyRotation;
public bool applyScale;
}
public ReplacementPreferences replacementPreferences;
EditorSettings.NamingScheme namingScheme;
public bool SearchWithTag = false;
public string TagForSearch = "Untagged";
public GameObject[] searchResult;
public bool SearchWithLayer = false;
public int LayerForSearch;
private Vector2 windowMinSize = new Vector2(450, 300);
private Vector2 windowMaxSize = new Vector2(800, 1000);
private Vector2 scrollPosition;
private static readonly IDictionary<System.Type, IComponentCopier> componentCopiers = new Dictionary<System.Type, IComponentCopier>();
private static readonly IDictionary<System.Type, ISet<string>> componentPartAvoiders = new Dictionary<System.Type, ISet<string>>();
static ReplaceWithPrefab()
{
RegisterComponentCopiers();
RegisterComponentPartAvoiders();
}
/// <summary>
/// Gets or creates a new Replace with Prefab window.
/// </summary>
[MenuItem("Sirandane/Production/Replace with Prefab")]
static void StartWindow()
{
ReplaceWithPrefab window = (ReplaceWithPrefab)GetWindow(typeof(ReplaceWithPrefab));
window.Show();
window.titleContent = new GUIContent("Replace with Prefab");
window.minSize = window.windowMinSize;
window.maxSize = window.windowMaxSize;
}
public ReplaceWithPrefab()
{
replacementPreferences.renameObjects = false;
replacementPreferences.orderHierarchyToPreview = false;
replacementPreferences.applyRotation = true;
replacementPreferences.applyScale = true;
}
/// <summary>
/// Handles getting the selected objects when the selection changes.
/// </summary>
void OnSelectionChange()
{
GetSelection();
Repaint();
}
/// <summary>
/// Draws the window content: object list, configuration and execution buttons.
/// </summary>
void OnGUI()
{
#region Draw Top Buttons
GUILayout.BeginHorizontal(EditorStyles.toolbar);
{
editMode = GUILayout.Toggle(editMode, new GUIContent("Start replacing", "Start using this feature"), EditorStyles.toggle);
GUILayout.FlexibleSpace();
}
GUILayout.EndHorizontal();
#endregion
#region "TAG LAYER"
SearchWithTag = GUILayout.Toggle(!SearchWithLayer ? SearchWithTag : false, "Apply Search By Tag", EditorStyles.toggle);
SearchWithLayer = GUILayout.Toggle(!SearchWithTag ? SearchWithLayer : false, "Apply Search By Layer");
if (SearchWithTag)
{
GUILayout.Space(5);
TagForSearch = EditorGUILayout.TagField("Set tag : ", TagForSearch);
}
else if (SearchWithLayer)
{
GUILayout.Space(5);
LayerForSearch = EditorGUILayout.LayerField("Set layer : ", LayerForSearch);
}
#endregion "TAG LAYER"
if (GUI.changed)
{
if (editMode)
GetSelection();
else
ResetPreview();
}
GUILayout.Space(10);
if (editMode)
{
namingScheme = EditorSettings.gameObjectNamingScheme;
RenamePreview();
#region Draw Prefab and List
GUILayout.Label("Prefab: ", EditorStyles.boldLabel);
prefab = (GameObject)EditorGUILayout.ObjectField(prefab, typeof(GameObject), true);
if (oldPrefab != prefab)
{
GetSelection();
oldPrefab = prefab;
}
GUILayout.Space(10);
if (prefab != null)
{
if (objectsToReplace.Count > 0)
{
GUILayout.Label(new GUIContent("Objects to be Replaced:", (!SearchWithTag && !SearchWithLayer) ? "Multi-select all the objects you want to replace with your Prefab" : ""), EditorStyles.boldLabel);
objectPreview.Sort(new NaturalComparer());
scrollPosition = GUILayout.BeginScrollView(scrollPosition, EditorStyles.helpBox);
{
GUILayout.BeginHorizontal(EditorStyles.helpBox);
{
string previewText = "No Preview";
if (replacementPreferences.renameObjects && !replacementPreferences.orderHierarchyToPreview)
previewText = "Preview of Renaming";
else if (replacementPreferences.orderHierarchyToPreview && !replacementPreferences.renameObjects)
previewText = "Preview of Hierarchy Order";
else if (replacementPreferences.orderHierarchyToPreview && replacementPreferences.renameObjects)
previewText = "Preview of Renaming and Hierarchy Order";
GUILayout.Label(previewText, EditorStyles.miniLabel);
}
GUILayout.EndHorizontal();
foreach (string go in objectPreview)
{
GUILayout.BeginHorizontal(EditorStyles.toolbar);
GUILayout.Label(go);
GUILayout.EndHorizontal();
}
GUILayout.Space(2);
}
GUILayout.EndScrollView();
GUILayout.Space(5);
replacementPreferences.renameObjects = GUILayout.Toggle(replacementPreferences.renameObjects, "Rename replaced objects", EditorStyles.toggle);
replacementPreferences.orderHierarchyToPreview = GUILayout.Toggle(replacementPreferences.orderHierarchyToPreview, "Oder hierarchy to preview", EditorStyles.toggle);
GUILayout.Space(10);
replacementPreferences.applyRotation = GUILayout.Toggle(replacementPreferences.applyRotation, "Apply rotation", EditorStyles.toggle);
replacementPreferences.applyScale = GUILayout.Toggle(replacementPreferences.applyScale, "Apply scale", EditorStyles.toggle);
}
else if (!SearchWithTag && !SearchWithLayer)
{
GUILayout.Label(new GUIContent("Multi-select all the objects you want to replace with your Prefab"), EditorStyles.boldLabel);
}
}
else
{
GUILayout.Label("Select a Prefab to replace objects with", EditorStyles.boldLabel);
}
#endregion
#region Draw Bottom Buttons
GUILayout.Space(5);
GUILayout.BeginHorizontal();
{
if (prefab != null && objectsToReplace.Count > 0)
{
if (GUILayout.Button("Apply"))
{
foreach (GameObject go in objectsToReplace)
{
Replace(go);
DestroyImmediate(go);
}
if (replacementPreferences.renameObjects)
{
Rename();
}
editMode = false;
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); // Important so that we don't forget to save!
}
else if (GUILayout.Button("Cancel"))
{
objectsToReplace.Clear();
objectPreview.Clear();
ResetPreview();
prefab = null;
}
}
}
GUILayout.EndHorizontal();
#endregion
}
else
{
objectsToReplace.Clear();
objectPreview.Clear();
newObjects.Clear();
prefab = null;
}
}
/// <summary>
/// Renames the gameObjects, adding numbering following the Naming Scheme Set in the Project Settings.
/// It checks for already used numbers.
/// </summary>
void Rename()
{
int count = 0;
List<int> ExistingNumbers = new List<int>();
SetExistingNumbers(newObjects, ExistingNumbers, namingScheme);
//Apply new names
foreach (GameObject go in newObjects)
{
count = GetCount(count, ExistingNumbers);
switch (namingScheme)
{
case EditorSettings.NamingScheme.SpaceParenthesis:
go.name = prefab.name + " (" + count + ")";
break;
case EditorSettings.NamingScheme.Dot:
go.name = prefab.name + "." + count;
break;
case EditorSettings.NamingScheme.Underscore:
go.name = prefab.name + "_" + count;
break;
}
}
}
/// <summary>
/// Renames the list of names, adding numbering following the Naming Scheme Set in the Project Settings.
/// It checks for already used numbers.
/// </summary>
void RenamePreview()
{
int count = 0;
List<int> ExistingNumbers = new List<int>();
objectPreview.Clear();
if (replacementPreferences.renameObjects)
{
SetExistingNumbers(objectsToReplace, ExistingNumbers, namingScheme);
}
//Apply new names
for (int n = 0; n < objectsToReplace.Count; n++)
{
if (replacementPreferences.renameObjects)
{
count = GetCount(count, ExistingNumbers);
switch (namingScheme)
{
case EditorSettings.NamingScheme.SpaceParenthesis:
objectPreview.Add(prefab.name + " (" + count + ")");
break;
case EditorSettings.NamingScheme.Dot:
objectPreview.Add(prefab.name + "." + count);
break;
case EditorSettings.NamingScheme.Underscore:
objectPreview.Add(prefab.name + "_" + count);
break;
}
}
else if (!replacementPreferences.renameObjects)
objectPreview.Add(objectsToReplace[n].name);
}
}
/// <summary>
/// Set existing numbers based on the naming scheme set in the project settings
/// </summary>
/// <param name="objects">The list of objects to get the names from</param>
/// <param name="ExistingNumbers">The list of ExistingNumbers to set</param>
/// <param name="namingScheme">The naming scheme to use</param>
void SetExistingNumbers(List<GameObject> objects, List<int> ExistingNumbers, EditorSettings.NamingScheme namingScheme)
{
foreach (GameObject obj in objects)
{
string name = obj.name;
if (name.Contains(prefab.name) && name.Any(char.IsDigit))
{
int num = 0;
switch (namingScheme)
{
case EditorSettings.NamingScheme.SpaceParenthesis:
char[] charsToTrim = { '(', ')' };
num = GetExistingNumber(name, name.Split(' '), charsToTrim);
break;
case EditorSettings.NamingScheme.Dot:
num = GetExistingNumber(name, name.Split('.'));
break;
case EditorSettings.NamingScheme.Underscore:
num = GetExistingNumber(name, name.Split('_'));
break;
}
if (num != 0)
ExistingNumbers.Add(num);
else
Debug.LogError("The selected object cannot be renamed, as there are several naming schemes used");
}
}
}
/// <summary>
/// Finds the "space" character in the name to identify where the number is, then if needed, strips provided extra characters.
/// </summary>
/// <param name="name">The name of the object</param>
/// <param name="splitChars">The string array</param>
/// <param name="charsToTrim">The extra characters to trim before converting the string to an int</param>
int GetExistingNumber(string name, string[] splitChars, char[] charsToTrim = null)
{
int count = 1;
if (splitChars.Length > 1)
{
string digit = splitChars[1]; // substring which contains number
//Get the substring that contains digits
while (GetDigits(digit) == "")
{
count++;
digit = GetDigits(splitChars[count]);
}
if (charsToTrim != null)
digit = digit.Trim(charsToTrim);
return int.Parse(GetDigits(digit)); // convert string to number
}
else
{
return int.Parse(GetDigits(name));
}
}
/// <summary>
/// The number to give the
/// </summary>
/// <param name="count">The name of the object</param>
/// <param name="ExistingNumbers">The existing numbers to keep</param>
int GetCount(int count, List<int> ExistingNumbers)
{
if (ExistingNumbers.Count > 0)
{
int i = 0;
while (i < ExistingNumbers.Count)
{
if (count == ExistingNumbers[i])
{
count++;
i = 0;
return count;
}
else
i++;
}
}
count++;
return count;
}
/// <summary>
/// Replaces a given gameObject with a previously chosen prefab.
/// </summary>
/// <param name="obj">The gameObject to replace.</param>
void Replace(GameObject obj)
{
GameObject newObject;
newObject = PrefabUtility.InstantiatePrefab(PrefabUtility.GetCorrespondingObjectFromSource(prefab)) as GameObject;
if (newObject == null) // if the prefab is chosen from the project browser and not the hierarchy, it is null
newObject = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
newObject.transform.SetParent(obj.transform.parent, true);
newObjects.Add(newObject);
CopyContentsToNew(obj, newObject);
Undo.RegisterCreatedObjectUndo(newObject, "Replaced Objects");
Undo.DestroyObjectImmediate(obj);
}
private void CopyContentsToNew(GameObject oldObject, GameObject newObject)
{
newObject.tag = oldObject.tag;
newObject.layer = oldObject.layer;
Component[] components = oldObject.GetComponents(typeof(Component));
foreach (Component component in components)
{
IComponentCopier copier = null;
System.Type componentType = component.GetType();
while (copier == null)
{
if (componentCopiers.ContainsKey(componentType))
copier = componentCopiers[componentType];
else if (componentType.BaseType == typeof(System.Object) ||
componentType.BaseType == null)
copier = defaultComponentCopier;
else
componentType = componentType.BaseType; // Components may be derivatives, so look up the tree.
}
copier.CopyComponent(replacementPreferences, component, newObject);
}
int childCount = oldObject.transform.childCount;
for (int i = 0; i < oldObject.transform.childCount; i++)
{
GameObject child = oldObject.transform.GetChild(i).gameObject;
GameObject newChild;
Transform newChildTransform = newObject.transform.Find(child.name);
if (newChildTransform == null)
{
newChild = new GameObject(child.name);
newChild.transform.SetParent(newObject.transform, false);
}
else
newChild = newChildTransform.gameObject;
CopyContentsToNew(child, newChild);
}
}
/// <summary>
/// Gets the currently selected game objects.
/// </summary>
void GetSelection()
{
namingScheme = EditorSettings.gameObjectNamingScheme;
if (editMode && Selection.activeGameObject != null && (prefab == null || (!SearchWithTag && !SearchWithLayer)))
{
if (prefab == null) // get the prefab 1st
{
PrefabAssetType t = PrefabUtility.GetPrefabAssetType(Selection.activeGameObject);
if (t == PrefabAssetType.Regular || t == PrefabAssetType.Variant)
{
prefab = Selection.activeGameObject;
oldPrefab = Selection.activeGameObject;
}
}
else // get the other objects
{
ResetPreview();
objectPreview.Clear();
objectsToReplace.Clear();
foreach (var obj in Selection.gameObjects)
{
if (obj != prefab)
objectsToReplace.Add(obj);
}
}
}
if (editMode && prefab != null && (SearchWithTag || SearchWithLayer))
{
if (SearchWithTag)
{
ResetPreview();
objectPreview.Clear();
objectsToReplace.Clear();
GameObject[] allGameObjects = GameObject.FindObjectsOfType<GameObject>();
foreach (var gg in allGameObjects)
{
if (gg.tag == TagForSearch)
{
if (gg != prefab)
objectsToReplace.Add(gg);
}
}
}
else if (SearchWithLayer)
{
ResetPreview();
objectPreview.Clear();
objectsToReplace.Clear();
GameObject[] allGameObjects = GameObject.FindObjectsOfType<GameObject>();
foreach (var gg in allGameObjects)
{
if (gg.layer == LayerForSearch)
{
if (gg != prefab)
objectsToReplace.Add(gg);
}
}
}
}
else if (editMode && Selection.activeGameObject == null && prefab != null && !SearchWithTag && !SearchWithLayer)
{
ResetPreview();
objectPreview.Clear();
objectsToReplace.Clear();
}
}
/// <summary>
/// Resets the gameObject preview.
/// </summary>
void ResetPreview()
{
if (newObjects != null)
{
foreach (GameObject go in newObjects)
{
DestroyImmediate(go);
}
}
newObjects.Clear();
}
/// <summary>
/// Handles window destruction.
/// </summary>
void OnDestroy()
{
ResetPreview();
}
/// <summary>
/// Takes all digits from a string and returns them as one string.
/// </summary>
/// <param name="text">The string to get the digits from</param>
/// <returns>A string of digits</returns>
string GetDigits(string text)
{
string digits = "";
for (int i = 0; i < text.Length; i++)
{
if (char.IsDigit(text[i]))
{
digits += text[i];
}
}
return digits;
}
/// <summary>
/// ASCII comparer class
/// </summary>
public class NaturalComparer : Comparer<string>, IDisposable
{
private Dictionary<string, string[]> table;
public NaturalComparer()
{
table = new Dictionary<string, string[]>();
}
public void Dispose()
{
table.Clear();
table = null;
}
public override int Compare(string x, string y)
{
if (x == y)
{
return 0;
}
string[] x1, y1;
if (!table.TryGetValue(x, out x1))
{
x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
table.Add(x, x1);
}
if (!table.TryGetValue(y, out y1))
{
y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
table.Add(y, y1);
}
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
{
return PartCompare(x1[i], y1[i]);
}
}
if (y1.Length > x1.Length)
{
return 1;
}
else if (x1.Length > y1.Length)
{
return -1;
}
else
{
return 0;
}
}
private static int PartCompare(string left, string right)
{
int x, y;
if (!int.TryParse(left, out x))
{
return left.CompareTo(right);
}
if (!int.TryParse(right, out y))
{
return left.CompareTo(right);
}
return x.CompareTo(y);
}
}
/// <summary>
/// Register component-specific copy objects for any components that require special handling.
/// </summary>
static void RegisterComponentCopiers()
{
componentCopiers.Add(typeof(Transform), new TransformComponentCopier());
componentCopiers.Add(typeof(MeshRenderer), new MeshRendererComponentCopier());
componentCopiers.Add(typeof(SkinnedMeshRenderer), new SkinnedMeshRendererComponentCopier());
}
/// <summary>
/// Register component-specific property names to avoid copying in a default manner.
/// </summary>
static void RegisterComponentPartAvoiders()
{
ISet<string> transformAvoiders = new HashSet<string>
{
"localRotation",
"localScale",
"name",
"parent"
};
componentPartAvoiders.Add(typeof(Transform), transformAvoiders);
}
// The interface for component-specific copiers from old to new GameObjects.
public interface IComponentCopier
{
void CopyComponent(ReplacementPreferences replacementPreferences, Component original, GameObject newObject);
}
// For anything that does not have a component-specific copier, or anything that does but wants to include default copy behaviour.
public class DefaultComponentCopier : IComponentCopier
{
public void CopyComponent(ReplacementPreferences replacementPreferences, Component original, GameObject newObject)
{
System.Type type = original.GetType();
ISet<string> partAvoiders = componentPartAvoiders.ContainsKey(type) ? componentPartAvoiders[type] : null;
var dst = newObject.GetComponent(type);
if (!dst)
dst = newObject.AddComponent(type);
var fields = type.GetFields();
foreach (var field in fields)
{
if (field.IsStatic)
continue;
if (partAvoiders != null && partAvoiders.Contains(field.Name))
continue;
field.SetValue(dst, field.GetValue(original));
}
var props = type.GetProperties();
foreach (var prop in props)
{
if (!prop.CanWrite || prop.Name == "name" || prop.Name == "parent")
continue;
if (partAvoiders != null && partAvoiders.Contains(prop.Name))
continue;
prop.SetValue(dst, prop.GetValue(original));
}
// NOTE: Some properties are references to other things and a prefab replacement can break them.
// TODO: Should we record any reference types in order to map them to new references later?
}
}
// Shared instance of default copier.
public static DefaultComponentCopier defaultComponentCopier = new DefaultComponentCopier();
/// <summary>
/// Transform-specific component copier.
/// </summary>
public class TransformComponentCopier : IComponentCopier
{
public void CopyComponent(ReplacementPreferences replacementPreferences, Component original, GameObject newObject)
{
Transform oldTransform = (Transform)original;
if (replacementPreferences.applyRotation)
newObject.transform.localRotation = oldTransform.localRotation;
if (replacementPreferences.applyScale)
newObject.transform.localScale = oldTransform.localScale;
if (!replacementPreferences.renameObjects)
newObject.transform.name = oldTransform.name;
if (!replacementPreferences.orderHierarchyToPreview)
newObject.transform.SetSiblingIndex(oldTransform.GetSiblingIndex());
defaultComponentCopier.CopyComponent(replacementPreferences, original, newObject);
}
}
/// <summary>
/// Special for-purpose component copier for mesh renderer.
/// </summary>
public class MeshRendererComponentCopier : IComponentCopier
{
public void CopyComponent(ReplacementPreferences replacementPreferences, Component original, GameObject newObject)
{
MeshRenderer meshRenderer = (MeshRenderer)original;
MeshRenderer newMeshRenderer = newObject.GetComponent<MeshRenderer>();
// QUESTION Should we instantiate one if one is not present?
if (newMeshRenderer)
{
if (meshRenderer.sharedMaterials.Length == newMeshRenderer.sharedMaterials.Length)
{
Material[] CacheMaterials = new Material[meshRenderer.sharedMaterials.Length];
for (int a = 0; a < meshRenderer.sharedMaterials.Length; a++)
CacheMaterials[a] = meshRenderer.sharedMaterials[a];
for (int b = 0; b < CacheMaterials.Length; b++)
newMeshRenderer.sharedMaterials[b] = CacheMaterials[b];
}
}
}
}
/// <summary>
/// Special for-purpose component copier for skinned mesh renderer.
/// </summary>
public class SkinnedMeshRendererComponentCopier : IComponentCopier
{
public void CopyComponent(ReplacementPreferences replacementPreferences, Component original, GameObject newObject)
{
SkinnedMeshRenderer meshRenderer = (SkinnedMeshRenderer)original;
SkinnedMeshRenderer newMeshRenderer = newObject.GetComponent<SkinnedMeshRenderer>();
// QUESTION Should we instantiate one if one is not present?
if (newMeshRenderer)
{
if (meshRenderer.sharedMaterials.Length == newMeshRenderer.sharedMaterials.Length)
{
Material[] CacheMaterials = new Material[meshRenderer.sharedMaterials.Length];
for (int a = 0; a < meshRenderer.sharedMaterials.Length; a++)
CacheMaterials[a] = meshRenderer.sharedMaterials[a];
for (int b = 0; b < CacheMaterials.Length; b++)
newMeshRenderer.sharedMaterials[b] = CacheMaterials[b];
}
}
}
}
}
}