/
PointFollowerInspector.cs
187 lines (160 loc) · 7.12 KB
/
PointFollowerInspector.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System.Collections;
using UnityEditor;
using UnityEngine;
namespace Spine.Unity.Editor {
using Editor = UnityEditor.Editor;
using Event = UnityEngine.Event;
[CustomEditor(typeof(PointFollower)), CanEditMultipleObjects]
public class PointFollowerInspector : Editor {
SerializedProperty slotName, pointAttachmentName, skeletonRenderer, followZPosition, followBoneRotation, followSkeletonFlip;
PointFollower targetPointFollower;
bool needsReset;
#region Context Menu Item
[MenuItem("CONTEXT/SkeletonRenderer/Add PointFollower GameObject")]
static void AddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer;
var go = EditorInstantiation.NewGameObject("PointFollower", true);
var t = go.transform;
t.SetParent(skeletonRenderer.transform);
t.localPosition = Vector3.zero;
var f = go.AddComponent<PointFollower>();
f.skeletonRenderer = skeletonRenderer;
EditorGUIUtility.PingObject(t);
Undo.RegisterCreatedObjectUndo(go, "Add PointFollower");
}
// Validate
[MenuItem("CONTEXT/SkeletonRenderer/Add PointFollower GameObject", true)]
static bool ValidateAddBoneFollowerGameObject (MenuCommand cmd) {
var skeletonRenderer = cmd.context as SkeletonRenderer;
return skeletonRenderer.valid;
}
#endregion
void OnEnable () {
skeletonRenderer = serializedObject.FindProperty("skeletonRenderer");
slotName = serializedObject.FindProperty("slotName");
pointAttachmentName = serializedObject.FindProperty("pointAttachmentName");
targetPointFollower = (PointFollower)target;
if (targetPointFollower.skeletonRenderer != null)
targetPointFollower.skeletonRenderer.Initialize(false);
if (!targetPointFollower.IsValid || needsReset) {
targetPointFollower.Initialize();
targetPointFollower.LateUpdate();
needsReset = false;
SceneView.RepaintAll();
}
}
public void OnSceneGUI () {
var tbf = target as PointFollower;
var skeletonRendererComponent = tbf.skeletonRenderer;
if (skeletonRendererComponent == null)
return;
var skeleton = skeletonRendererComponent.skeleton;
var skeletonTransform = skeletonRendererComponent.transform;
if (string.IsNullOrEmpty(pointAttachmentName.stringValue)) {
// Draw all active PointAttachments in the current skin
var currentSkin = skeleton.Skin;
if (currentSkin != skeleton.Data.DefaultSkin) DrawPointsInSkin(skeleton.Data.DefaultSkin, skeleton, skeletonTransform);
if (currentSkin != null) DrawPointsInSkin(currentSkin, skeleton, skeletonTransform);
} else {
Slot slot = skeleton.FindSlot(slotName.stringValue);
if (slot != null) {
int slotIndex = slot.Data.Index;
var point = skeleton.GetAttachment(slotIndex, pointAttachmentName.stringValue) as PointAttachment;
if (point != null) {
DrawPointAttachmentWithLabel(point, slot.Bone, skeletonTransform);
}
}
}
}
static void DrawPointsInSkin (Skin skin, Skeleton skeleton, Transform transform) {
foreach (var skinEntry in skin.Attachments) {
var attachment = skinEntry.Attachment as PointAttachment;
if (attachment != null) {
var slot = skeleton.Slots.Items[skinEntry.SlotIndex];
DrawPointAttachmentWithLabel(attachment, slot.Bone, transform);
}
}
}
static void DrawPointAttachmentWithLabel (PointAttachment point, Bone bone, Transform transform) {
Vector3 labelOffset = new Vector3(0f, -0.2f, 0f);
SpineHandles.DrawPointAttachment(bone, point, transform);
Handles.Label(labelOffset + point.GetWorldPosition(bone, transform), point.Name, SpineHandles.PointNameStyle);
}
override public void OnInspectorGUI () {
if (serializedObject.isEditingMultipleObjects) {
if (needsReset) {
needsReset = false;
foreach (var o in targets) {
var bf = (BoneFollower)o;
bf.Initialize();
bf.LateUpdate();
}
SceneView.RepaintAll();
}
EditorGUI.BeginChangeCheck();
DrawDefaultInspector();
needsReset |= EditorGUI.EndChangeCheck();
return;
}
if (needsReset && Event.current.type == EventType.Layout) {
targetPointFollower.Initialize();
targetPointFollower.LateUpdate();
needsReset = false;
SceneView.RepaintAll();
}
serializedObject.Update();
DrawDefaultInspector();
// Find Renderer
if (skeletonRenderer.objectReferenceValue == null) {
SkeletonRenderer parentRenderer = targetPointFollower.GetComponentInParent<SkeletonRenderer>();
if (parentRenderer != null && parentRenderer.gameObject != targetPointFollower.gameObject) {
skeletonRenderer.objectReferenceValue = parentRenderer;
Debug.Log("Inspector automatically assigned PointFollower.SkeletonRenderer");
}
}
var skeletonRendererReference = skeletonRenderer.objectReferenceValue as SkeletonRenderer;
if (skeletonRendererReference != null) {
if (skeletonRendererReference.gameObject == targetPointFollower.gameObject) {
skeletonRenderer.objectReferenceValue = null;
EditorUtility.DisplayDialog("Invalid assignment.", "PointFollower can only follow a skeleton on a separate GameObject.\n\nCreate a new GameObject for your PointFollower, or choose a SkeletonRenderer from a different GameObject.", "Ok");
}
}
if (!targetPointFollower.IsValid) {
needsReset = true;
}
var current = Event.current;
bool wasUndo = (current.type == EventType.ValidateCommand && current.commandName == "UndoRedoPerformed");
if (wasUndo)
targetPointFollower.Initialize();
serializedObject.ApplyModifiedProperties();
}
}
}