BoneFollower.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using System.IO;
  32. using System.Collections.Generic;
  33. using UnityEngine;
  34. using Spine;
  35. /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
  36. [ExecuteInEditMode]
  37. [AddComponentMenu("Spine/BoneFollower")]
  38. public class BoneFollower : MonoBehaviour
  39. {
  40. [System.NonSerialized]
  41. public bool
  42. valid;
  43. public SkeletonRenderer skeletonRenderer;
  44. public Bone bone;
  45. public bool followZPosition = true;
  46. public bool followBoneRotation = true;
  47. public SkeletonRenderer SkeletonRenderer
  48. {
  49. get { return skeletonRenderer; }
  50. set
  51. {
  52. skeletonRenderer = value;
  53. Reset();
  54. }
  55. }
  56. /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
  57. public String boneName;
  58. public bool resetOnAwake = true;
  59. protected Transform cachedTransform;
  60. protected Transform skeletonTransform;
  61. public void HandleResetRenderer(SkeletonRenderer skeletonRenderer)
  62. {
  63. Reset();
  64. }
  65. public void Reset()
  66. {
  67. bone = null;
  68. cachedTransform = transform;
  69. valid = skeletonRenderer != null && skeletonRenderer.valid;
  70. if (!valid)
  71. return;
  72. skeletonTransform = skeletonRenderer.transform;
  73. skeletonRenderer.OnReset -= HandleResetRenderer;
  74. skeletonRenderer.OnReset += HandleResetRenderer;
  75. if (Application.isEditor)
  76. DoUpdate();
  77. }
  78. void OnDestroy()
  79. {
  80. //cleanup
  81. if (skeletonRenderer != null)
  82. skeletonRenderer.OnReset -= HandleResetRenderer;
  83. }
  84. public void Awake()
  85. {
  86. if (resetOnAwake)
  87. Reset();
  88. }
  89. void LateUpdate()
  90. {
  91. DoUpdate();
  92. }
  93. public void DoUpdate()
  94. {
  95. if (!valid)
  96. {
  97. Reset();
  98. return;
  99. }
  100. if (bone == null)
  101. {
  102. if (boneName == null || boneName.Length == 0)
  103. return;
  104. bone = skeletonRenderer.skeleton.FindBone(boneName);
  105. if (bone == null)
  106. {
  107. Debug.LogError("Bone not found: " + boneName, this);
  108. return;
  109. }
  110. else
  111. {
  112. }
  113. }
  114. Spine.Skeleton skeleton = skeletonRenderer.skeleton;
  115. float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
  116. if (cachedTransform.parent == skeletonTransform)
  117. {
  118. cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : cachedTransform.localPosition.z);
  119. if (followBoneRotation)
  120. {
  121. Vector3 rotation = cachedTransform.localRotation.eulerAngles;
  122. cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation * flipRotation);
  123. }
  124. }
  125. else
  126. {
  127. Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f));
  128. if (!followZPosition)
  129. targetWorldPosition.z = cachedTransform.position.z;
  130. cachedTransform.position = targetWorldPosition;
  131. if (followBoneRotation)
  132. {
  133. Vector3 rotation = skeletonTransform.rotation.eulerAngles;
  134. cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y, skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation));
  135. }
  136. }
  137. }
  138. }