mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[unity] Fix SkeletonUtility and BoundingBox functionality.
This commit is contained in:
parent
4dc462e7a8
commit
78855cdb70
@ -28,6 +28,8 @@
|
|||||||
* POSSIBILITY OF SUCH DAMAGE.
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#pragma warning disable 0219
|
||||||
|
|
||||||
// Contributed by: Mitch Thompson
|
// Contributed by: Mitch Thompson
|
||||||
|
|
||||||
#define SPINE_SKELETONANIMATOR
|
#define SPINE_SKELETONANIMATOR
|
||||||
|
|||||||
@ -180,7 +180,7 @@ namespace Spine.Unity {
|
|||||||
for (int i = 0; i < bufferTargetSize; i++) {
|
for (int i = 0; i < bufferTargetSize; i++) {
|
||||||
int j = i * 2;
|
int j = i * 2;
|
||||||
float x = floats[j] - bwx, y = floats[j+1] - bwy;
|
float x = floats[j] - bwx, y = floats[j+1] - bwy;
|
||||||
buffer[i] = new Vector2(x * ia + y * ic, x * ib + y * id);
|
buffer[i] = new Vector2(x * ia + y * ib, x * ic + y * id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -184,7 +184,7 @@ namespace Spine.Unity.Editor {
|
|||||||
using (new EditorGUI.DisabledGroupScope(multiObject || boundingBoxTable.Count == 0)) {
|
using (new EditorGUI.DisabledGroupScope(multiObject || boundingBoxTable.Count == 0)) {
|
||||||
EditorGUILayout.LabelField(new GUIContent("Bounding Boxes", SpineEditorUtilities.Icons.boundingBox), EditorStyles.boldLabel);
|
EditorGUILayout.LabelField(new GUIContent("Bounding Boxes", SpineEditorUtilities.Icons.boundingBox), EditorStyles.boldLabel);
|
||||||
|
|
||||||
foreach(var entry in boundingBoxTable){
|
foreach (var entry in boundingBoxTable){
|
||||||
Slot slot = entry.Key;
|
Slot slot = entry.Key;
|
||||||
var boundingBoxes = entry.Value;
|
var boundingBoxes = entry.Value;
|
||||||
|
|
||||||
@ -195,19 +195,21 @@ namespace Spine.Unity.Editor {
|
|||||||
foreach (var box in boundingBoxes) {
|
foreach (var box in boundingBoxes) {
|
||||||
using (new GUILayout.HorizontalScope()) {
|
using (new GUILayout.HorizontalScope()) {
|
||||||
GUILayout.Space(30);
|
GUILayout.Space(30);
|
||||||
if (GUILayout.Button(box.Name, GUILayout.Width(200))) {
|
string buttonLabel = box.IsWeighted() ? box.Name + " (!)" : box.Name;
|
||||||
var child = utilityBone.transform.FindChild("[BoundingBox]" + box.Name);
|
if (GUILayout.Button(buttonLabel, GUILayout.Width(200))) {
|
||||||
if (child != null) {
|
utilityBone.bone.Skeleton.UpdateWorldTransform();
|
||||||
var originalCollider = child.GetComponent<PolygonCollider2D>();
|
var bbTransform = utilityBone.transform.FindChild("[BoundingBox]" + box.Name);
|
||||||
var updatedCollider = SkeletonUtility.AddBoundingBoxAsComponent(box, slot, child.gameObject, originalCollider.isTrigger);
|
if (bbTransform != null) {
|
||||||
originalCollider.points = updatedCollider.points;
|
var originalCollider = bbTransform.GetComponent<PolygonCollider2D>();
|
||||||
if (EditorApplication.isPlaying)
|
if (originalCollider != null)
|
||||||
Destroy(updatedCollider);
|
SkeletonUtility.SetColliderPointsLocal(originalCollider, slot, box);
|
||||||
else
|
else
|
||||||
DestroyImmediate(updatedCollider);
|
SkeletonUtility.AddBoundingBoxAsComponent(box, slot, bbTransform.gameObject);
|
||||||
} else {
|
} else {
|
||||||
utilityBone.AddBoundingBox(currentSkinName, slot.Data.Name, box.Name);
|
var newPolygonCollider = SkeletonUtility.AddBoundingBoxGameObject(null, box, slot, utilityBone.transform);
|
||||||
|
bbTransform = newPolygonCollider.transform;
|
||||||
}
|
}
|
||||||
|
EditorGUIUtility.PingObject(bbTransform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -40,39 +40,55 @@ namespace Spine.Unity {
|
|||||||
public class SkeletonUtility : MonoBehaviour {
|
public class SkeletonUtility : MonoBehaviour {
|
||||||
|
|
||||||
#region BoundingBoxAttachment
|
#region BoundingBoxAttachment
|
||||||
public static PolygonCollider2D AddBoundingBox (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
|
public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
|
||||||
Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
|
Skin skin = string.IsNullOrEmpty(skinName) ? skeleton.data.defaultSkin : skeleton.data.FindSkin(skinName);
|
||||||
if (skin == null) {
|
if (skin == null) {
|
||||||
Debug.LogError("Skin " + skinName + " not found!");
|
Debug.LogError("Skin " + skinName + " not found!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int slotIndex = skeleton.FindSlotIndex(slotName);
|
||||||
var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
|
var attachment = skin.GetAttachment(skeleton.FindSlotIndex(slotName), attachmentName);
|
||||||
var box = attachment as BoundingBoxAttachment;
|
if (attachment == null) {
|
||||||
if (box != null) {
|
Debug.LogFormat("Attachment in slot '{0}' named '{1}' not found in skin '{2}'.", slotName, attachmentName, skin.name);
|
||||||
var go = new GameObject("[BoundingBox]" + attachmentName);
|
return null;
|
||||||
var got = go.transform;
|
|
||||||
got.parent = parent;
|
|
||||||
got.localPosition = Vector3.zero;
|
|
||||||
got.localRotation = Quaternion.identity;
|
|
||||||
got.localScale = Vector3.one;
|
|
||||||
var slot = skeleton.FindSlot(slotName);
|
|
||||||
return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
var box = attachment as BoundingBoxAttachment;
|
||||||
|
if (box != null) {
|
||||||
|
var slot = skeleton.FindSlot(slotName);
|
||||||
|
return AddBoundingBoxGameObject(box.Name, box, slot, parent, isTrigger);
|
||||||
|
} else {
|
||||||
|
Debug.LogFormat("Attachment '{0}' was not a Bounding Box.", attachmentName);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PolygonCollider2D AddBoundingBoxGameObject (string name, BoundingBoxAttachment box, Slot slot, Transform parent, bool isTrigger = true) {
|
||||||
|
var go = new GameObject("[BoundingBox]" + (string.IsNullOrEmpty(name) ? box.Name : name));
|
||||||
|
var got = go.transform;
|
||||||
|
got.parent = parent;
|
||||||
|
got.localPosition = Vector3.zero;
|
||||||
|
got.localRotation = Quaternion.identity;
|
||||||
|
got.localScale = Vector3.one;
|
||||||
|
return AddBoundingBoxAsComponent(box, slot, go, isTrigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PolygonCollider2D AddBoundingBoxAsComponent (BoundingBoxAttachment box, Slot slot, GameObject gameObject, bool isTrigger = true) {
|
public static PolygonCollider2D AddBoundingBoxAsComponent (BoundingBoxAttachment box, Slot slot, GameObject gameObject, bool isTrigger = true) {
|
||||||
if (box == null) return null;
|
if (box == null) return null;
|
||||||
if (box.IsWeighted()) Debug.LogWarning("UnityEngine.PolygonCollider2D does not support weighted or animated points. Collider will not be animated. If you want to use it as a collider, please remove weights and animations from the bounding box in Spine editor.");
|
|
||||||
var verts = box.GetLocalVertices(slot, null);
|
|
||||||
var collider = gameObject.AddComponent<PolygonCollider2D>();
|
var collider = gameObject.AddComponent<PolygonCollider2D>();
|
||||||
collider.isTrigger = isTrigger;
|
collider.isTrigger = isTrigger;
|
||||||
collider.SetPath(0, verts);
|
SetColliderPointsLocal(collider, slot, box);
|
||||||
return collider;
|
return collider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void SetColliderPointsLocal (PolygonCollider2D collider, Slot slot, BoundingBoxAttachment box) {
|
||||||
|
if (box == null) return;
|
||||||
|
if (box.IsWeighted()) Debug.LogWarning("UnityEngine.PolygonCollider2D does not support weighted or animated points. Collider points will not be animated and may have incorrect orientation. If you want to use it as a collider, please remove weights and animations from the bounding box in Spine editor.");
|
||||||
|
var verts = box.GetLocalVertices(slot, null);
|
||||||
|
collider.SetPath(0, verts);
|
||||||
|
}
|
||||||
|
|
||||||
public static Bounds GetBoundingBoxBounds (BoundingBoxAttachment boundingBox, float depth = 0) {
|
public static Bounds GetBoundingBoxBounds (BoundingBoxAttachment boundingBox, float depth = 0) {
|
||||||
float[] floats = boundingBox.Vertices;
|
float[] floats = boundingBox.Vertices;
|
||||||
int floatCount = floats.Length;
|
int floatCount = floats.Length;
|
||||||
|
|||||||
@ -63,9 +63,7 @@ namespace Spine.Unity {
|
|||||||
Transform cachedTransform;
|
Transform cachedTransform;
|
||||||
Transform skeletonTransform;
|
Transform skeletonTransform;
|
||||||
bool incompatibleTransformMode;
|
bool incompatibleTransformMode;
|
||||||
public bool IncompatibleTransformMode {
|
public bool IncompatibleTransformMode { get { return incompatibleTransformMode; } }
|
||||||
get { return incompatibleTransformMode; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Reset () {
|
public void Reset () {
|
||||||
bone = null;
|
bone = null;
|
||||||
@ -132,7 +130,7 @@ namespace Spine.Unity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (scale) {
|
if (scale) {
|
||||||
cachedTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);//, bone.WorldSignX);
|
cachedTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
|
||||||
incompatibleTransformMode = BoneTransformModeIncompatible(bone);
|
incompatibleTransformMode = BoneTransformModeIncompatible(bone);
|
||||||
}
|
}
|
||||||
} else if (mode == Mode.Override) {
|
} else if (mode == Mode.Override) {
|
||||||
@ -193,7 +191,7 @@ namespace Spine.Unity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
|
public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
|
||||||
SkeletonUtility.AddBoundingBox(bone.skeleton, skinName, slotName, attachmentName, transform);
|
SkeletonUtility.AddBoundingBoxGameObject(bone.skeleton, skinName, slotName, attachmentName, transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user