SkeletonUtilityBone.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
  30. #define NEW_PREFAB_SYSTEM
  31. #endif
  32. using UnityEngine;
  33. namespace Spine.Unity {
  34. /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
  35. #if NEW_PREFAB_SYSTEM
  36. [ExecuteAlways]
  37. #else
  38. [ExecuteInEditMode]
  39. #endif
  40. [AddComponentMenu("Spine/SkeletonUtilityBone")]
  41. public class SkeletonUtilityBone : MonoBehaviour {
  42. public enum Mode {
  43. Follow,
  44. Override
  45. }
  46. public enum UpdatePhase {
  47. Local,
  48. World,
  49. Complete
  50. }
  51. #region Inspector
  52. /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
  53. public string boneName;
  54. public Transform parentReference;
  55. public Mode mode;
  56. public bool position, rotation, scale, zPosition = true;
  57. [Range(0f, 1f)]
  58. public float overrideAlpha = 1;
  59. #endregion
  60. public SkeletonUtility hierarchy;
  61. [System.NonSerialized] public Bone bone;
  62. [System.NonSerialized] public bool transformLerpComplete;
  63. [System.NonSerialized] public bool valid;
  64. Transform cachedTransform;
  65. Transform skeletonTransform;
  66. bool incompatibleTransformMode;
  67. public bool IncompatibleTransformMode { get { return incompatibleTransformMode; } }
  68. public void Reset () {
  69. bone = null;
  70. cachedTransform = transform;
  71. valid = hierarchy != null && hierarchy.IsValid;
  72. if (!valid)
  73. return;
  74. skeletonTransform = hierarchy.transform;
  75. hierarchy.OnReset -= HandleOnReset;
  76. hierarchy.OnReset += HandleOnReset;
  77. DoUpdate(UpdatePhase.Local);
  78. }
  79. void OnEnable () {
  80. if (hierarchy == null) hierarchy = transform.GetComponentInParent<SkeletonUtility>();
  81. if (hierarchy == null) return;
  82. hierarchy.RegisterBone(this);
  83. hierarchy.OnReset += HandleOnReset;
  84. }
  85. void HandleOnReset () {
  86. Reset();
  87. }
  88. void OnDisable () {
  89. if (hierarchy != null) {
  90. hierarchy.OnReset -= HandleOnReset;
  91. hierarchy.UnregisterBone(this);
  92. }
  93. }
  94. public void DoUpdate (UpdatePhase phase) {
  95. if (!valid) {
  96. Reset();
  97. return;
  98. }
  99. var skeleton = hierarchy.Skeleton;
  100. if (bone == null) {
  101. if (string.IsNullOrEmpty(boneName)) return;
  102. bone = skeleton.FindBone(boneName);
  103. if (bone == null) {
  104. Debug.LogError("Bone not found: " + boneName, this);
  105. return;
  106. }
  107. }
  108. if (!bone.Active) return;
  109. float positionScale = hierarchy.PositionScale;
  110. var thisTransform = cachedTransform;
  111. float skeletonFlipRotation = Mathf.Sign(skeleton.ScaleX * skeleton.ScaleY);
  112. if (mode == Mode.Follow) {
  113. switch (phase) {
  114. case UpdatePhase.Local:
  115. if (position)
  116. thisTransform.localPosition = new Vector3(bone.x * positionScale, bone.y * positionScale, 0);
  117. if (rotation) {
  118. if (bone.data.transformMode.InheritsRotation()) {
  119. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.rotation);
  120. } else {
  121. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  122. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  123. }
  124. }
  125. if (scale) {
  126. thisTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
  127. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  128. }
  129. break;
  130. case UpdatePhase.World:
  131. case UpdatePhase.Complete:
  132. // Use Applied transform values (ax, ay, AppliedRotation, ascale) if world values were modified by constraints.
  133. if (!bone.appliedValid) {
  134. bone.UpdateAppliedTransform();
  135. }
  136. if (position)
  137. thisTransform.localPosition = new Vector3(bone.ax * positionScale, bone.ay * positionScale, 0);
  138. if (rotation) {
  139. if (bone.data.transformMode.InheritsRotation()) {
  140. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation);
  141. } else {
  142. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  143. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  144. }
  145. }
  146. if (scale) {
  147. thisTransform.localScale = new Vector3(bone.ascaleX, bone.ascaleY, 1f);
  148. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  149. }
  150. break;
  151. }
  152. } else if (mode == Mode.Override) {
  153. if (transformLerpComplete)
  154. return;
  155. if (parentReference == null) {
  156. if (position) {
  157. Vector3 clp = thisTransform.localPosition / positionScale;
  158. bone.x = Mathf.Lerp(bone.x, clp.x, overrideAlpha);
  159. bone.y = Mathf.Lerp(bone.y, clp.y, overrideAlpha);
  160. }
  161. if (rotation) {
  162. float angle = Mathf.LerpAngle(bone.Rotation, thisTransform.localRotation.eulerAngles.z, overrideAlpha);
  163. bone.Rotation = angle;
  164. bone.AppliedRotation = angle;
  165. }
  166. if (scale) {
  167. Vector3 cls = thisTransform.localScale;
  168. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  169. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  170. }
  171. } else {
  172. if (transformLerpComplete)
  173. return;
  174. if (position) {
  175. Vector3 pos = parentReference.InverseTransformPoint(thisTransform.position) / positionScale;
  176. bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
  177. bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha);
  178. }
  179. if (rotation) {
  180. float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(thisTransform.up)).eulerAngles.z, overrideAlpha);
  181. bone.Rotation = angle;
  182. bone.AppliedRotation = angle;
  183. }
  184. if (scale) {
  185. Vector3 cls = thisTransform.localScale;
  186. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  187. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  188. }
  189. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  190. }
  191. transformLerpComplete = true;
  192. }
  193. }
  194. public static bool BoneTransformModeIncompatible (Bone bone) {
  195. return !bone.data.transformMode.InheritsScale();
  196. }
  197. public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
  198. SkeletonUtility.AddBoneRigidbody2D(transform.gameObject);
  199. SkeletonUtility.AddBoundingBoxGameObject(bone.skeleton, skinName, slotName, attachmentName, transform);
  200. }
  201. #if UNITY_EDITOR
  202. void OnDrawGizmos () {
  203. if (IncompatibleTransformMode)
  204. Gizmos.DrawIcon(transform.position + new Vector3(0, 0.128f, 0), "icon-warning");
  205. }
  206. #endif
  207. }
  208. }