-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathCustomRectTransformCopyInspector.cs
114 lines (96 loc) · 3.56 KB
/
CustomRectTransformCopyInspector.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
#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEngine;
namespace UnityEditor
{
[CustomEditor(typeof(RectTransform), true)]
[CanEditMultipleObjects]
public class CustomRectTransformCopyInspector : Editor
{
// Unity's built-in editor
Editor defaultEditor = null;
RectTransform rectTransform;
private static RectTransformData copiedData;
void OnEnable()
{
// Use reflection to get the default Unity RectTransform editor
defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.RectTransformEditor, UnityEditor"));
rectTransform = target as RectTransform;
}
void OnDisable()
{
// Destroy the default editor to avoid memory leaks
if (defaultEditor != null)
{
MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (disableMethod != null)
disableMethod.Invoke(defaultEditor, null);
DestroyImmediate(defaultEditor);
}
}
public override void OnInspectorGUI()
{
// Draw Unity's default RectTransform Inspector
defaultEditor.OnInspectorGUI();
// Add Copy and Paste buttons
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
if (GUILayout.Button("C", GUILayout.Width(30))) // Copy
{
CopyRectTransform(rectTransform);
}
if (GUILayout.Button("P", GUILayout.Width(30))) // Paste
{
PasteRectTransform(rectTransform);
}
GUILayout.EndHorizontal();
}
private void CopyRectTransform(RectTransform rectTransform)
{
copiedData = new RectTransformData(rectTransform);
Debug.Log("RectTransform copied!");
}
private void PasteRectTransform(RectTransform rectTransform)
{
if (copiedData == null)
{
Debug.LogWarning("No RectTransform data to paste!");
return;
}
Undo.RecordObject(rectTransform, "Paste RectTransform");
copiedData.ApplyTo(rectTransform);
Debug.Log("RectTransform pasted!");
EditorUtility.SetDirty(rectTransform);
}
private class RectTransformData
{
public Vector2 anchorMin;
public Vector2 anchorMax;
public Vector2 anchoredPosition;
public Vector2 sizeDelta;
public Vector2 pivot;
public Quaternion rotation;
public RectTransformData(RectTransform rectTransform)
{
anchorMin = rectTransform.anchorMin;
anchorMax = rectTransform.anchorMax;
anchoredPosition = rectTransform.anchoredPosition;
sizeDelta = rectTransform.sizeDelta;
pivot = rectTransform.pivot;
rotation = rectTransform.rotation;
}
public void ApplyTo(RectTransform rectTransform)
{
rectTransform.anchorMin = anchorMin;
rectTransform.anchorMax = anchorMax;
rectTransform.anchoredPosition = anchoredPosition;
rectTransform.sizeDelta = sizeDelta;
rectTransform.pivot = pivot;
rectTransform.rotation = rotation;
}
}
}
}
#endif