void DisposeColliders () { #if UNITY_EDITOR var colliders = GetComponents<PolygonCollider2D>(); if (colliders.Length == 0) return; if (Application.isPlaying) { foreach (var c in colliders) { if (c != null) Destroy(c); } } else { foreach (var c in colliders) if (c != null) DestroyImmediate(c); } #else foreach (PolygonCollider2D c in colliderTable.Values) if (c != null) Destroy(c); #endif slot = null; currentAttachment = null; currentAttachmentName = null; currentCollider = null; colliderTable.Clear(); nameTable.Clear(); }
/// <summary>Sets the current collider to match attachment.</summary> /// <param name="attachment">If the attachment is not a bounding box, it will be treated as null.</param> void MatchAttachment (Attachment attachment) { var bbAttachment = attachment as BoundingBoxAttachment; if (BoundingBoxFollower.DebugMessages && attachment != null && bbAttachment == null) Debug.LogWarning("BoundingBoxFollower tried to match a non-boundingbox attachment. It will treat it as null."); if (currentCollider != null) currentCollider.enabled = false; if (bbAttachment == null) { currentCollider = null; } else { currentCollider = colliderTable[bbAttachment]; currentCollider.enabled = true; } currentAttachment = bbAttachment; currentAttachmentName = currentAttachment == null ? null : nameTable[bbAttachment]; }
public void ClearState () { if (colliderTable != null) foreach (var col in colliderTable.Values) col.enabled = false; currentAttachment = null; currentAttachmentName = null; currentCollider = null; }
void SetCurrent (BoundingBoxAttachment attachment) { if (currentCollider) currentCollider.enabled = false; if (attachment != null) { currentCollider = colliderTable[attachment]; currentCollider.enabled = true; } else { currentCollider = null; } currentAttachment = attachment; currentAttachmentName = currentAttachment == null ? null : attachmentNameTable[attachment]; }
/// <summary> /// Initialize and instantiate the BoundingBoxFollowerGraphic colliders. This is method checks if the BoundingBoxFollowerGraphic has already been initialized for the skeleton instance and slotName and prevents overwriting unless it detects a new setup.</summary> public void Initialize(bool overwrite = false) { if (skeletonGraphic == null) { return; } skeletonGraphic.Initialize(false); if (string.IsNullOrEmpty(slotName)) { return; } // Don't reinitialize if the setup did not change. if (!overwrite && colliderTable.Count > 0 && slot != null && // Slot is set and colliders already populated. skeletonGraphic.Skeleton == slot.Skeleton && // Skeleton object did not change. slotName == slot.Data.Name // Slot object did not change. ) { return; } slot = null; currentAttachment = null; currentAttachmentName = null; currentCollider = null; colliderTable.Clear(); nameTable.Clear(); var skeleton = skeletonGraphic.Skeleton; if (skeleton == null) { return; } slot = skeleton.FindSlot(slotName); if (slot == null) { if (BoundingBoxFollowerGraphic.DebugMessages) { Debug.LogWarning(string.Format("Slot '{0}' not found for BoundingBoxFollowerGraphic on '{1}'. (Previous colliders were disposed.)", slotName, this.gameObject.name)); } return; } int slotIndex = slot.Data.Index; int requiredCollidersCount = 0; var colliders = GetComponents <PolygonCollider2D>(); if (this.gameObject.activeInHierarchy) { var canvas = skeletonGraphic.canvas; if (canvas == null) { canvas = skeletonGraphic.GetComponentInParent <Canvas>(); } float scale = canvas != null ? canvas.referencePixelsPerUnit : 100.0f; foreach (var skin in skeleton.Data.Skins) { AddCollidersForSkin(skin, slotIndex, colliders, scale, ref requiredCollidersCount); } if (skeleton.Skin != null) { AddCollidersForSkin(skeleton.Skin, slotIndex, colliders, scale, ref requiredCollidersCount); } } DisposeExcessCollidersAfter(requiredCollidersCount); if (BoundingBoxFollowerGraphic.DebugMessages) { bool valid = colliderTable.Count != 0; 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."); } } } }
public static BoundingBoxAttachment GetClone (this BoundingBoxAttachment o) { var ba = new BoundingBoxAttachment(o.Name); CloneVertexAttachment(o, ba); return o; }
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); }