-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomUILayout
More file actions
109 lines (93 loc) · 3.04 KB
/
CustomUILayout
File metadata and controls
109 lines (93 loc) · 3.04 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
using UnityEngine;
using UnityEngine.Serialization;
namespace AnimationOrTween
{
[System.Serializable]
public class ChildLayoutConfig
{
public Vector3 Pos;
public Vector3 Rot;
}
/// <summary>
/// 子对象按照摆好的位置和朝向布局
/// </summary>
[ExecuteInEditMode]
public class CustomUILayout : MonoBehaviour
{
public ChildLayoutConfig[] ChildConfigs;
void Update()
{
UpdateLayout();
}
public void UpdateLayout()
{
if (ChildConfigs != null && ChildConfigs.Length > 0)
{
int Index = 0;
int childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
var child = transform.GetChild(i);
if (Index >= ChildConfigs.Length)
{
break;
}
if (child.gameObject.activeSelf)
{
var config = ChildConfigs[Index];
if (child.transform.localPosition != config.Pos)
{
child.transform.localPosition = config.Pos;
}
if (child.transform.localEulerAngles != config.Rot)
{
child.transform.localEulerAngles = config.Rot;
}
Index++;
}
}
}
}
}
}
using AnimationOrTween;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(CustomUILayout), true)]
public class CustomLayoutEditor : Editor
{
public override void OnInspectorGUI()
{
GUILayout.Space(6f);
CustomUILayout script = target as CustomUILayout;
base.OnInspectorGUI();
}
void OnSceneGUI()
{
CustomUILayout script = target as CustomUILayout;
if (script)
{
for (int i = 0; i < script.ChildConfigs.Length; i++)
{
var config = script.ChildConfigs[i];
var worldPos = script.transform.TransformPoint(config.Pos);
var worldDir = script.transform.TransformVector(config.Rot);
Handles.Label(worldPos,"Child"+i);
var worldRotation = new Quaternion();
worldRotation.eulerAngles = worldDir;
if (Tools.current == Tool.Move)
{
config.Pos =script.transform.InverseTransformPoint(Handles.PositionHandle(worldPos, worldRotation));
}
else if(Tools.current==Tool.Rotate)
{
config.Rot = script.transform.InverseTransformVector(Handles.RotationHandle(worldRotation, worldPos).eulerAngles);
}
if (GUI.changed)
{
script.UpdateLayout();
}
}
}
}
}