BoundingBoxFollower.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using Spine;
  4. using UnityEngine;
  5. [ExecuteInEditMode]
  6. public class BoundingBoxFollower : MonoBehaviour
  7. {
  8. public string CurrentAttachmentName
  9. {
  10. get
  11. {
  12. return this.currentAttachmentName;
  13. }
  14. }
  15. public BoundingBoxAttachment CurrentAttachment
  16. {
  17. get
  18. {
  19. return this.currentAttachment;
  20. }
  21. }
  22. public PolygonCollider2D CurrentCollider
  23. {
  24. get
  25. {
  26. return this.currentCollider;
  27. }
  28. }
  29. public Slot Slot
  30. {
  31. get
  32. {
  33. return this.slot;
  34. }
  35. }
  36. private void OnEnable()
  37. {
  38. this.ClearColliders();
  39. if (this.skeletonRenderer == null)
  40. {
  41. this.skeletonRenderer = base.GetComponentInParent<SkeletonRenderer>();
  42. }
  43. if (this.skeletonRenderer != null)
  44. {
  45. SkeletonRenderer skeletonRenderer = this.skeletonRenderer;
  46. skeletonRenderer.OnReset = (SkeletonRenderer.SkeletonRendererDelegate)Delegate.Remove(skeletonRenderer.OnReset, new SkeletonRenderer.SkeletonRendererDelegate(this.HandleReset));
  47. SkeletonRenderer skeletonRenderer2 = this.skeletonRenderer;
  48. skeletonRenderer2.OnReset = (SkeletonRenderer.SkeletonRendererDelegate)Delegate.Combine(skeletonRenderer2.OnReset, new SkeletonRenderer.SkeletonRendererDelegate(this.HandleReset));
  49. }
  50. }
  51. private void OnDisable()
  52. {
  53. SkeletonRenderer skeletonRenderer = this.skeletonRenderer;
  54. skeletonRenderer.OnReset = (SkeletonRenderer.SkeletonRendererDelegate)Delegate.Remove(skeletonRenderer.OnReset, new SkeletonRenderer.SkeletonRendererDelegate(this.HandleReset));
  55. }
  56. private void Start()
  57. {
  58. if (!this.hasReset && this.skeletonRenderer != null)
  59. {
  60. this.HandleReset(this.skeletonRenderer);
  61. }
  62. }
  63. public void HandleReset(SkeletonRenderer renderer)
  64. {
  65. if (this.slotName == null || this.slotName == string.Empty)
  66. {
  67. return;
  68. }
  69. this.hasReset = true;
  70. this.ClearColliders();
  71. this.colliderTable.Clear();
  72. if (this.skeletonRenderer.skeleton == null)
  73. {
  74. SkeletonRenderer skeletonRenderer = this.skeletonRenderer;
  75. skeletonRenderer.OnReset = (SkeletonRenderer.SkeletonRendererDelegate)Delegate.Remove(skeletonRenderer.OnReset, new SkeletonRenderer.SkeletonRendererDelegate(this.HandleReset));
  76. this.skeletonRenderer.Reset();
  77. SkeletonRenderer skeletonRenderer2 = this.skeletonRenderer;
  78. skeletonRenderer2.OnReset = (SkeletonRenderer.SkeletonRendererDelegate)Delegate.Combine(skeletonRenderer2.OnReset, new SkeletonRenderer.SkeletonRendererDelegate(this.HandleReset));
  79. }
  80. Skeleton skeleton = this.skeletonRenderer.skeleton;
  81. this.slot = skeleton.FindSlot(this.slotName);
  82. int slotIndex = skeleton.FindSlotIndex(this.slotName);
  83. foreach (Skin skin in skeleton.Data.Skins)
  84. {
  85. List<string> list = new List<string>();
  86. skin.FindNamesForSlot(slotIndex, list);
  87. foreach (string text in list)
  88. {
  89. Attachment attachment = skin.GetAttachment(slotIndex, text);
  90. if (attachment is BoundingBoxAttachment)
  91. {
  92. PolygonCollider2D polygonCollider2D = SkeletonUtility.AddBoundingBoxAsComponent((BoundingBoxAttachment)attachment, base.gameObject, true);
  93. polygonCollider2D.enabled = false;
  94. polygonCollider2D.hideFlags = HideFlags.HideInInspector;
  95. this.colliderTable.Add((BoundingBoxAttachment)attachment, polygonCollider2D);
  96. this.attachmentNameTable.Add((BoundingBoxAttachment)attachment, text);
  97. }
  98. }
  99. }
  100. if (this.colliderTable.Count == 0)
  101. {
  102. this.valid = false;
  103. }
  104. else
  105. {
  106. this.valid = true;
  107. }
  108. if (!this.valid)
  109. {
  110. UnityEngine.Debug.LogWarning("Bounding Box Follower not valid! Slot [" + this.slotName + "] does not contain any Bounding Box Attachments!");
  111. }
  112. }
  113. private void ClearColliders()
  114. {
  115. PolygonCollider2D[] components = base.GetComponents<PolygonCollider2D>();
  116. if (Application.isPlaying)
  117. {
  118. foreach (PolygonCollider2D obj in components)
  119. {
  120. UnityEngine.Object.Destroy(obj);
  121. }
  122. }
  123. else
  124. {
  125. foreach (PolygonCollider2D obj2 in components)
  126. {
  127. UnityEngine.Object.DestroyImmediate(obj2);
  128. }
  129. }
  130. this.colliderTable.Clear();
  131. this.attachmentNameTable.Clear();
  132. }
  133. private void LateUpdate()
  134. {
  135. if (!this.skeletonRenderer.valid)
  136. {
  137. return;
  138. }
  139. if (this.slot != null && this.slot.Attachment != this.currentAttachment)
  140. {
  141. this.SetCurrent((BoundingBoxAttachment)this.slot.Attachment);
  142. }
  143. }
  144. private void SetCurrent(BoundingBoxAttachment attachment)
  145. {
  146. if (this.currentCollider)
  147. {
  148. this.currentCollider.enabled = false;
  149. }
  150. if (attachment != null)
  151. {
  152. this.currentCollider = this.colliderTable[attachment];
  153. this.currentCollider.enabled = true;
  154. }
  155. else
  156. {
  157. this.currentCollider = null;
  158. }
  159. this.currentAttachment = attachment;
  160. this.currentAttachmentName = ((this.currentAttachment != null) ? this.attachmentNameTable[attachment] : null);
  161. }
  162. public SkeletonRenderer skeletonRenderer;
  163. [SpineSlot("", "skeletonRenderer")]
  164. public string slotName;
  165. [Tooltip("LOL JK, Someone else do it!")]
  166. public bool use3DMeshCollider;
  167. private Slot slot;
  168. private BoundingBoxAttachment currentAttachment;
  169. private PolygonCollider2D currentCollider;
  170. private string currentAttachmentName;
  171. private bool valid;
  172. private bool hasReset;
  173. public Dictionary<BoundingBoxAttachment, PolygonCollider2D> colliderTable = new Dictionary<BoundingBoxAttachment, PolygonCollider2D>();
  174. public Dictionary<BoundingBoxAttachment, string> attachmentNameTable = new Dictionary<BoundingBoxAttachment, string>();
  175. }