|
@@ -32,28 +32,30 @@ using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
|
|
|
namespace Spine.Unity {
|
|
namespace Spine.Unity {
|
|
|
|
+
|
|
[ExecuteInEditMode]
|
|
[ExecuteInEditMode]
|
|
public class BoundingBoxFollower : MonoBehaviour {
|
|
public class BoundingBoxFollower : MonoBehaviour {
|
|
-
|
|
|
|
|
|
+ #region Inspector
|
|
public SkeletonRenderer skeletonRenderer;
|
|
public SkeletonRenderer skeletonRenderer;
|
|
-
|
|
|
|
[SpineSlot(dataField: "skeletonRenderer", containsBoundingBoxes: true)]
|
|
[SpineSlot(dataField: "skeletonRenderer", containsBoundingBoxes: true)]
|
|
public string slotName;
|
|
public string slotName;
|
|
|
|
+ #endregion
|
|
|
|
|
|
Slot slot;
|
|
Slot slot;
|
|
BoundingBoxAttachment currentAttachment;
|
|
BoundingBoxAttachment currentAttachment;
|
|
- PolygonCollider2D currentCollider;
|
|
|
|
string currentAttachmentName;
|
|
string currentAttachmentName;
|
|
|
|
+ PolygonCollider2D currentCollider;
|
|
|
|
+
|
|
bool valid = false;
|
|
bool valid = false;
|
|
bool hasReset;
|
|
bool hasReset;
|
|
|
|
|
|
- public Dictionary<BoundingBoxAttachment, PolygonCollider2D> colliderTable = new Dictionary<BoundingBoxAttachment, PolygonCollider2D>();
|
|
|
|
- public Dictionary<BoundingBoxAttachment, string> attachmentNameTable = new Dictionary<BoundingBoxAttachment, string>();
|
|
|
|
|
|
+ public readonly Dictionary<BoundingBoxAttachment, PolygonCollider2D> colliderTable = new Dictionary<BoundingBoxAttachment, PolygonCollider2D>();
|
|
|
|
+ public readonly Dictionary<BoundingBoxAttachment, string> attachmentNameTable = new Dictionary<BoundingBoxAttachment, string>();
|
|
|
|
|
|
- public string CurrentAttachmentName { get { return currentAttachmentName; } }
|
|
|
|
|
|
+ public Slot Slot { get { return slot; } }
|
|
public BoundingBoxAttachment CurrentAttachment { get { return currentAttachment; } }
|
|
public BoundingBoxAttachment CurrentAttachment { get { return currentAttachment; } }
|
|
|
|
+ public string CurrentAttachmentName { get { return currentAttachmentName; } }
|
|
public PolygonCollider2D CurrentCollider { get { return currentCollider; } }
|
|
public PolygonCollider2D CurrentCollider { get { return currentCollider; } }
|
|
- public Slot Slot { get { return slot; } }
|
|
|
|
|
|
|
|
void OnEnable () {
|
|
void OnEnable () {
|
|
ClearColliders();
|
|
ClearColliders();
|
|
@@ -62,74 +64,91 @@ namespace Spine.Unity {
|
|
skeletonRenderer = GetComponentInParent<SkeletonRenderer>();
|
|
skeletonRenderer = GetComponentInParent<SkeletonRenderer>();
|
|
|
|
|
|
if (skeletonRenderer != null) {
|
|
if (skeletonRenderer != null) {
|
|
- skeletonRenderer.OnRebuild -= HandleReset;
|
|
|
|
- skeletonRenderer.OnRebuild += HandleReset;
|
|
|
|
|
|
+ skeletonRenderer.OnRebuild -= HandleRebuild;
|
|
|
|
+ skeletonRenderer.OnRebuild += HandleRebuild;
|
|
|
|
|
|
if (hasReset)
|
|
if (hasReset)
|
|
- HandleReset(skeletonRenderer);
|
|
|
|
|
|
+ HandleRebuild(skeletonRenderer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void OnDisable () {
|
|
void OnDisable () {
|
|
- skeletonRenderer.OnRebuild -= HandleReset;
|
|
|
|
|
|
+ skeletonRenderer.OnRebuild -= HandleRebuild;
|
|
}
|
|
}
|
|
|
|
|
|
void Start () {
|
|
void Start () {
|
|
if (!hasReset && skeletonRenderer != null)
|
|
if (!hasReset && skeletonRenderer != null)
|
|
- HandleReset(skeletonRenderer);
|
|
|
|
|
|
+ HandleRebuild(skeletonRenderer);
|
|
}
|
|
}
|
|
|
|
|
|
- public void HandleReset (SkeletonRenderer renderer) {
|
|
|
|
|
|
+ public void HandleRebuild (SkeletonRenderer renderer) {
|
|
if (string.IsNullOrEmpty(slotName))
|
|
if (string.IsNullOrEmpty(slotName))
|
|
return;
|
|
return;
|
|
|
|
|
|
hasReset = true;
|
|
hasReset = true;
|
|
-
|
|
|
|
ClearColliders();
|
|
ClearColliders();
|
|
colliderTable.Clear();
|
|
colliderTable.Clear();
|
|
|
|
|
|
if (skeletonRenderer.skeleton == null) {
|
|
if (skeletonRenderer.skeleton == null) {
|
|
- skeletonRenderer.OnRebuild -= HandleReset;
|
|
|
|
|
|
+ skeletonRenderer.OnRebuild -= HandleRebuild;
|
|
skeletonRenderer.Initialize(false);
|
|
skeletonRenderer.Initialize(false);
|
|
- skeletonRenderer.OnRebuild += HandleReset;
|
|
|
|
|
|
+ skeletonRenderer.OnRebuild += HandleRebuild;
|
|
}
|
|
}
|
|
|
|
|
|
var skeleton = skeletonRenderer.skeleton;
|
|
var skeleton = skeletonRenderer.skeleton;
|
|
slot = skeleton.FindSlot(slotName);
|
|
slot = skeleton.FindSlot(slotName);
|
|
int slotIndex = skeleton.FindSlotIndex(slotName);
|
|
int slotIndex = skeleton.FindSlotIndex(slotName);
|
|
|
|
|
|
- foreach (var skin in skeleton.Data.Skins) {
|
|
|
|
- var attachmentNames = new List<string>();
|
|
|
|
- skin.FindNamesForSlot(slotIndex, attachmentNames);
|
|
|
|
-
|
|
|
|
- foreach (var attachmentName in attachmentNames) {
|
|
|
|
- var attachment = skin.GetAttachment(slotIndex, attachmentName);
|
|
|
|
- var boundingBoxAttachment = attachment as BoundingBoxAttachment;
|
|
|
|
- if (boundingBoxAttachment != null) {
|
|
|
|
- var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(boundingBoxAttachment, gameObject, true);
|
|
|
|
- bbCollider.enabled = false;
|
|
|
|
- bbCollider.hideFlags = HideFlags.HideInInspector;
|
|
|
|
- colliderTable.Add(boundingBoxAttachment, bbCollider);
|
|
|
|
- attachmentNameTable.Add(boundingBoxAttachment, attachmentName);
|
|
|
|
|
|
+ if (this.gameObject.activeInHierarchy) {
|
|
|
|
+ foreach (var skin in skeleton.Data.Skins) {
|
|
|
|
+ var attachmentNames = new List<string>();
|
|
|
|
+ skin.FindNamesForSlot(slotIndex, attachmentNames);
|
|
|
|
+
|
|
|
|
+ foreach (var attachmentName in attachmentNames) {
|
|
|
|
+ var attachment = skin.GetAttachment(slotIndex, attachmentName);
|
|
|
|
+ var boundingBoxAttachment = attachment as BoundingBoxAttachment;
|
|
|
|
+ if (boundingBoxAttachment != null) {
|
|
|
|
+ var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(boundingBoxAttachment, gameObject, true);
|
|
|
|
+ bbCollider.enabled = false;
|
|
|
|
+ bbCollider.hideFlags = HideFlags.NotEditable;
|
|
|
|
+ colliderTable.Add(boundingBoxAttachment, bbCollider);
|
|
|
|
+ attachmentNameTable.Add(boundingBoxAttachment, attachmentName);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if UNITY_EDITOR
|
|
valid = colliderTable.Count != 0;
|
|
valid = colliderTable.Count != 0;
|
|
-
|
|
|
|
- if (!valid)
|
|
|
|
- Debug.LogWarning("Bounding Box Follower not valid! Slot [" + slotName + "] does not contain any Bounding Box Attachments!");
|
|
|
|
|
|
+ if (!valid) {
|
|
|
|
+ if (this.gameObject.activeInHierarchy)
|
|
|
|
+ Debug.LogWarning("Bounding Box Follower not valid! Slot [" + slotName + "] does not contain any Bounding Box Attachments!");
|
|
|
|
+ else
|
|
|
|
+ Debug.LogWarning("Bounding Box Follower tried to rebuild as a prefab.");
|
|
|
|
+ }
|
|
|
|
+#endif
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
void ClearColliders () {
|
|
void ClearColliders () {
|
|
var colliders = GetComponents<PolygonCollider2D>();
|
|
var colliders = GetComponents<PolygonCollider2D>();
|
|
|
|
+ if (colliders.Length == 0) return;
|
|
|
|
+
|
|
|
|
+#if UNITY_EDITOR
|
|
if (Application.isPlaying) {
|
|
if (Application.isPlaying) {
|
|
- foreach (var c in colliders)
|
|
|
|
- Destroy(c);
|
|
|
|
|
|
+ foreach (var c in colliders) {
|
|
|
|
+ if (c != null)
|
|
|
|
+ Destroy(c);
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
foreach (var c in colliders)
|
|
foreach (var c in colliders)
|
|
DestroyImmediate(c);
|
|
DestroyImmediate(c);
|
|
}
|
|
}
|
|
|
|
+#else
|
|
|
|
+ foreach (var c in colliders)
|
|
|
|
+ if (c != null)
|
|
|
|
+ Destroy(c);
|
|
|
|
+#endif
|
|
|
|
|
|
colliderTable.Clear();
|
|
colliderTable.Clear();
|
|
attachmentNameTable.Clear();
|
|
attachmentNameTable.Clear();
|
|
@@ -139,21 +158,19 @@ namespace Spine.Unity {
|
|
if (!skeletonRenderer.valid)
|
|
if (!skeletonRenderer.valid)
|
|
return;
|
|
return;
|
|
|
|
|
|
- if (slot != null) {
|
|
|
|
- if (slot.Attachment != currentAttachment)
|
|
|
|
- SetCurrent((BoundingBoxAttachment)slot.Attachment);
|
|
|
|
- }
|
|
|
|
|
|
+ if (slot != null && slot.Attachment != currentAttachment)
|
|
|
|
+ SetCurrent((BoundingBoxAttachment)slot.Attachment);
|
|
}
|
|
}
|
|
|
|
|
|
void SetCurrent (BoundingBoxAttachment attachment) {
|
|
void SetCurrent (BoundingBoxAttachment attachment) {
|
|
- if (currentCollider)
|
|
|
|
|
|
+ if (currentCollider != null)
|
|
currentCollider.enabled = false;
|
|
currentCollider.enabled = false;
|
|
|
|
|
|
- if (attachment != null) {
|
|
|
|
|
|
+ if (attachment == null) {
|
|
|
|
+ currentCollider = null;
|
|
|
|
+ } else {
|
|
currentCollider = colliderTable[attachment];
|
|
currentCollider = colliderTable[attachment];
|
|
currentCollider.enabled = true;
|
|
currentCollider.enabled = true;
|
|
- } else {
|
|
|
|
- currentCollider = null;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
currentAttachment = attachment;
|
|
currentAttachment = attachment;
|