SkeletonUtilityBone.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. [HelpURL("http://esotericsoftware.com/spine-unity#SkeletonUtilityBone")]
  42. public class SkeletonUtilityBone : MonoBehaviour {
  43. public enum Mode {
  44. Follow,
  45. Override
  46. }
  47. public enum UpdatePhase {
  48. Local,
  49. World,
  50. Complete
  51. }
  52. #region Inspector
  53. /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
  54. public string boneName;
  55. public Transform parentReference;
  56. public Mode mode;
  57. public bool position, rotation, scale, zPosition = true;
  58. [Range(0f, 1f)]
  59. public float overrideAlpha = 1;
  60. #endregion
  61. public SkeletonUtility hierarchy;
  62. [System.NonSerialized] public Bone bone;
  63. [System.NonSerialized] public bool transformLerpComplete;
  64. [System.NonSerialized] public bool valid;
  65. Transform cachedTransform;
  66. Transform skeletonTransform;
  67. bool incompatibleTransformMode;
  68. public bool IncompatibleTransformMode { get { return incompatibleTransformMode; } }
  69. public void Reset () {
  70. bone = null;
  71. cachedTransform = transform;
  72. valid = hierarchy != null && hierarchy.IsValid;
  73. if (!valid)
  74. return;
  75. skeletonTransform = hierarchy.transform;
  76. hierarchy.OnReset -= HandleOnReset;
  77. hierarchy.OnReset += HandleOnReset;
  78. DoUpdate(UpdatePhase.Local);
  79. }
  80. void OnEnable () {
  81. if (hierarchy == null) hierarchy = transform.GetComponentInParent<SkeletonUtility>();
  82. if (hierarchy == null) return;
  83. hierarchy.RegisterBone(this);
  84. hierarchy.OnReset += HandleOnReset;
  85. }
  86. void HandleOnReset () {
  87. Reset();
  88. }
  89. void OnDisable () {
  90. if (hierarchy != null) {
  91. hierarchy.OnReset -= HandleOnReset;
  92. hierarchy.UnregisterBone(this);
  93. }
  94. }
  95. public void DoUpdate (UpdatePhase phase) {
  96. if (!valid) {
  97. Reset();
  98. return;
  99. }
  100. var skeleton = hierarchy.Skeleton;
  101. if (bone == null) {
  102. if (string.IsNullOrEmpty(boneName)) return;
  103. bone = skeleton.FindBone(boneName);
  104. if (bone == null) {
  105. Debug.LogError("Bone not found: " + boneName, this);
  106. return;
  107. }
  108. }
  109. if (!bone.Active) return;
  110. float positionScale = hierarchy.PositionScale;
  111. var thisTransform = cachedTransform;
  112. float skeletonFlipRotation = Mathf.Sign(skeleton.ScaleX * skeleton.ScaleY);
  113. if (mode == Mode.Follow) {
  114. switch (phase) {
  115. case UpdatePhase.Local:
  116. if (position)
  117. thisTransform.localPosition = new Vector3(bone.x * positionScale, bone.y * positionScale, 0);
  118. if (rotation) {
  119. if (bone.data.transformMode.InheritsRotation()) {
  120. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.rotation);
  121. } else {
  122. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  123. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  124. }
  125. }
  126. if (scale) {
  127. thisTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
  128. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  129. }
  130. break;
  131. case UpdatePhase.World:
  132. case UpdatePhase.Complete:
  133. // Use Applied transform values (ax, ay, AppliedRotation, ascale) if world values were modified by constraints.
  134. if (!bone.appliedValid) {
  135. bone.UpdateAppliedTransform();
  136. }
  137. if (position)
  138. thisTransform.localPosition = new Vector3(bone.ax * positionScale, bone.ay * positionScale, 0);
  139. if (rotation) {
  140. if (bone.data.transformMode.InheritsRotation()) {
  141. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation);
  142. } else {
  143. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  144. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  145. }
  146. }
  147. if (scale) {
  148. thisTransform.localScale = new Vector3(bone.ascaleX, bone.ascaleY, 1f);
  149. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  150. }
  151. break;
  152. }
  153. } else if (mode == Mode.Override) {
  154. if (transformLerpComplete)
  155. return;
  156. if (parentReference == null) {
  157. if (position) {
  158. Vector3 clp = thisTransform.localPosition / positionScale;
  159. bone.x = Mathf.Lerp(bone.x, clp.x, overrideAlpha);
  160. bone.y = Mathf.Lerp(bone.y, clp.y, overrideAlpha);
  161. }
  162. if (rotation) {
  163. float angle = Mathf.LerpAngle(bone.Rotation, thisTransform.localRotation.eulerAngles.z, overrideAlpha);
  164. bone.Rotation = angle;
  165. bone.AppliedRotation = angle;
  166. }
  167. if (scale) {
  168. Vector3 cls = thisTransform.localScale;
  169. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  170. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  171. }
  172. } else {
  173. if (transformLerpComplete)
  174. return;
  175. if (position) {
  176. Vector3 pos = parentReference.InverseTransformPoint(thisTransform.position) / positionScale;
  177. bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
  178. bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha);
  179. }
  180. if (rotation) {
  181. float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(thisTransform.up)).eulerAngles.z, overrideAlpha);
  182. bone.Rotation = angle;
  183. bone.AppliedRotation = angle;
  184. }
  185. if (scale) {
  186. Vector3 cls = thisTransform.localScale;
  187. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  188. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  189. }
  190. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  191. }
  192. transformLerpComplete = true;
  193. }
  194. }
  195. public static bool BoneTransformModeIncompatible (Bone bone) {
  196. return !bone.data.transformMode.InheritsScale();
  197. }
  198. public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
  199. SkeletonUtility.AddBoneRigidbody2D(transform.gameObject);
  200. SkeletonUtility.AddBoundingBoxGameObject(bone.skeleton, skinName, slotName, attachmentName, transform);
  201. }
  202. #if UNITY_EDITOR
  203. void OnDrawGizmos () {
  204. if (IncompatibleTransformMode)
  205. Gizmos.DrawIcon(transform.position + new Vector3(0, 0.128f, 0), "icon-warning");
  206. }
  207. #endif
  208. }
  209. }