-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathRectTransformCloner.cs
More file actions
151 lines (129 loc) · 5.58 KB
/
RectTransformCloner.cs
File metadata and controls
151 lines (129 loc) · 5.58 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
147
148
149
150
151
// clones UI RectTransform values from one GameObject to another in Unity Editor (only for identical hierarchy)
using UnityEngine;
using UnityEditor;
using TMPro;
using UnityEngine.UI;
namespace UnityLibrary.Tools
{
public class RectTransformCloner : EditorWindow
{
private GameObject source;
private GameObject target;
private bool requireIdenticalNames = true;
private bool cloneTMPAlignment = false;
[MenuItem("Tools/RectTransform Cloner")]
private static void ShowWindow()
{
var window = GetWindow<RectTransformCloner>();
window.titleContent = new GUIContent("RectTransform Cloner");
window.Show();
}
private void OnGUI()
{
GUILayout.Label("Clone RectTransform", EditorStyles.boldLabel);
source = (GameObject)EditorGUILayout.ObjectField("Source", source, typeof(GameObject), true);
target = (GameObject)EditorGUILayout.ObjectField("Target", target, typeof(GameObject), true);
requireIdenticalNames = EditorGUILayout.Toggle("Require Identical Names", requireIdenticalNames);
cloneTMPAlignment = EditorGUILayout.Toggle("Clone TMP Alignment", cloneTMPAlignment);
if (GUILayout.Button("Clone RectTransforms"))
{
if (source == null || target == null)
{
Debug.LogError("Source and Target must be assigned.");
return;
}
string errorMessage;
if (!CompareHierarchies(source.transform, target.transform, out errorMessage))
{
Debug.LogError("Source and Target hierarchies do not match!\n" + errorMessage, target);
return;
}
Undo.RegisterFullObjectHierarchyUndo(target, "Clone RectTransform Values");
CopyRectTransforms(source.transform, target.transform);
Debug.Log("RectTransform values cloned successfully.", target);
if (target.transform.parent != null)
{
RectTransform parentRect = target.transform.parent as RectTransform;
if (parentRect != null)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(parentRect);
}
else
{
Debug.LogWarning("Target's parent is not a RectTransform, cannot force layout rebuild.", target);
}
}
else
{
Debug.LogWarning("Target has no parent, cannot force layout rebuild.", target);
}
EditorUtility.SetDirty(target);
SceneView.RepaintAll();
}
}
private bool CompareHierarchies(Transform source, Transform target, out string errorMessage)
{
errorMessage = "";
if (source.childCount != target.childCount)
{
errorMessage = $"Child count mismatch at {GetTransformPath(source)}: Source has {source.childCount}, Target has {target.childCount}";
return false;
}
for (int i = 0; i < source.childCount; i++)
{
var sourceChild = source.GetChild(i);
var targetChild = target.GetChild(i);
if (requireIdenticalNames && sourceChild.name != targetChild.name)
{
errorMessage = $"Child name mismatch at {GetTransformPath(sourceChild)}: Source has '{sourceChild.name}', Target has '{targetChild.name}'";
return false;
}
if (!CompareHierarchies(sourceChild, targetChild, out errorMessage))
{
return false;
}
}
return true;
}
private void CopyRectTransforms(Transform source, Transform target)
{
var sourceRect = source as RectTransform;
var targetRect = target as RectTransform;
if (sourceRect != null && targetRect != null)
{
CopyRectTransformValues(sourceRect, targetRect);
if (cloneTMPAlignment)
{
var sourceTMP = source.GetComponent<TextMeshProUGUI>();
var targetTMP = target.GetComponent<TextMeshProUGUI>();
if (sourceTMP != null && targetTMP != null)
{
Undo.RecordObject(targetTMP, "Clone TMP Alignment");
targetTMP.alignment = sourceTMP.alignment;
}
}
}
for (int i = 0; i < source.childCount; i++)
{
CopyRectTransforms(source.GetChild(i), target.GetChild(i));
}
}
private void CopyRectTransformValues(RectTransform source, RectTransform target)
{
target.anchoredPosition = source.anchoredPosition;
target.sizeDelta = source.sizeDelta;
target.anchorMin = source.anchorMin;
target.anchorMax = source.anchorMax;
target.pivot = source.pivot;
target.localRotation = source.localRotation;
target.localScale = source.localScale;
target.localPosition = source.localPosition;
}
private string GetTransformPath(Transform t)
{
if (t.parent == null)
return t.name;
return GetTransformPath(t.parent) + "/" + t.name;
}
}
}