-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlindCopyTransforms.cs
85 lines (69 loc) · 2.28 KB
/
BlindCopyTransforms.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
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
using static UnityEngine.EventSystems.EventTrigger;
public class BlindCopyTransforms : Editor
{
private static List<Transform> AllTransforms = new List<Transform>();
[MenuItem("GameObject/Kanna's Tools/Transforms/Copy All", false, 10)]
private static void Copy()
{
if (Selection.activeGameObject != null)
{
AllTransforms = Selection.activeGameObject.GetComponentsInChildren<Transform>(true).Where(o => o != Selection.activeGameObject).OrderBy(GetParentCount).ToList();
}
}
[MenuItem("GameObject/Kanna's Tools/Transforms/Paste Un-Found", false, 10)]
private static void Paste()
{
if (Selection.activeGameObject != null)
{
foreach (var transform in AllTransforms)
{
var FullPath = GetPath(transform);
if (!Selection.activeGameObject.transform.Find(FullPath))
{
transform.SetParent(transform.parent != null ? Selection.activeGameObject.transform.Find(GetPath(transform.parent)) : Selection.activeGameObject.transform);
}
}
}
}
private static string GetPath(Transform transform)
{
var PathToCreate = "";
var CurrentObject = transform;
while (CurrentObject != transform.root) // Create Path String - Loop
{
if (CurrentObject == null || string.IsNullOrWhiteSpace(CurrentObject.name))
{
break;
}
if (string.IsNullOrWhiteSpace(PathToCreate))
{
PathToCreate = CurrentObject.name;
}
else
{
PathToCreate = CurrentObject.name + "/" + PathToCreate;
}
CurrentObject = CurrentObject.parent;
}
return PathToCreate;
}
private static int GetParentCount(Transform transform)
{
var count = 0;
var CurrentObject = transform;
while (CurrentObject != transform.root)
{
count++;
CurrentObject = CurrentObject.parent;
}
return count;
}
}
#endif