Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84cf73c

Browse files
Create CustomRectTransformCopyInspector.cs
1 parent 9e21581 commit 84cf73c

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.Reflection;
4+
using UnityEngine;
5+
6+
namespace UnityEditor
7+
{
8+
[CustomEditor(typeof(RectTransform), true)]
9+
[CanEditMultipleObjects]
10+
public class CustomRectTransformCopyInspector : Editor
11+
{
12+
// Unity's built-in editor
13+
Editor defaultEditor = null;
14+
RectTransform rectTransform;
15+
16+
private static RectTransformData copiedData;
17+
18+
void OnEnable()
19+
{
20+
// Use reflection to get the default Unity RectTransform editor
21+
defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.RectTransformEditor, UnityEditor"));
22+
rectTransform = target as RectTransform;
23+
}
24+
25+
void OnDisable()
26+
{
27+
// Destroy the default editor to avoid memory leaks
28+
if (defaultEditor != null)
29+
{
30+
MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable",
31+
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
32+
if (disableMethod != null)
33+
disableMethod.Invoke(defaultEditor, null);
34+
35+
DestroyImmediate(defaultEditor);
36+
}
37+
}
38+
39+
public override void OnInspectorGUI()
40+
{
41+
// Draw Unity's default RectTransform Inspector
42+
defaultEditor.OnInspectorGUI();
43+
44+
// Add Copy and Paste buttons
45+
EditorGUILayout.Space();
46+
GUILayout.BeginHorizontal();
47+
48+
if (GUILayout.Button("C", GUILayout.Width(30))) // Copy
49+
{
50+
CopyRectTransform(rectTransform);
51+
}
52+
53+
if (GUILayout.Button("P", GUILayout.Width(30))) // Paste
54+
{
55+
PasteRectTransform(rectTransform);
56+
}
57+
58+
GUILayout.EndHorizontal();
59+
}
60+
61+
private void CopyRectTransform(RectTransform rectTransform)
62+
{
63+
copiedData = new RectTransformData(rectTransform);
64+
Debug.Log("RectTransform copied!");
65+
}
66+
67+
private void PasteRectTransform(RectTransform rectTransform)
68+
{
69+
if (copiedData == null)
70+
{
71+
Debug.LogWarning("No RectTransform data to paste!");
72+
return;
73+
}
74+
75+
Undo.RecordObject(rectTransform, "Paste RectTransform");
76+
77+
copiedData.ApplyTo(rectTransform);
78+
Debug.Log("RectTransform pasted!");
79+
80+
EditorUtility.SetDirty(rectTransform);
81+
}
82+
83+
private class RectTransformData
84+
{
85+
public Vector2 anchorMin;
86+
public Vector2 anchorMax;
87+
public Vector2 anchoredPosition;
88+
public Vector2 sizeDelta;
89+
public Vector2 pivot;
90+
public Quaternion rotation;
91+
92+
public RectTransformData(RectTransform rectTransform)
93+
{
94+
anchorMin = rectTransform.anchorMin;
95+
anchorMax = rectTransform.anchorMax;
96+
anchoredPosition = rectTransform.anchoredPosition;
97+
sizeDelta = rectTransform.sizeDelta;
98+
pivot = rectTransform.pivot;
99+
rotation = rectTransform.rotation;
100+
}
101+
102+
public void ApplyTo(RectTransform rectTransform)
103+
{
104+
rectTransform.anchorMin = anchorMin;
105+
rectTransform.anchorMax = anchorMax;
106+
rectTransform.anchoredPosition = anchoredPosition;
107+
rectTransform.sizeDelta = sizeDelta;
108+
rectTransform.pivot = pivot;
109+
rectTransform.rotation = rotation;
110+
}
111+
}
112+
}
113+
}
114+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /