SkeletonUtilityBone.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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. Skeleton 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. Transform 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,
  118. zPosition ? 0 : thisTransform.localPosition.z);
  119. if (rotation) {
  120. if (bone.Data.Inherit.InheritsRotation()) {
  121. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.Rotation);
  122. } else {
  123. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  124. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  125. }
  126. }
  127. if (scale) {
  128. thisTransform.localScale = new Vector3(bone.ScaleX, bone.ScaleY, 1f);
  129. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  130. }
  131. break;
  132. case UpdatePhase.World:
  133. case UpdatePhase.Complete:
  134. if (position)
  135. thisTransform.localPosition = new Vector3(bone.AX * positionScale, bone.AY * positionScale,
  136. zPosition ? 0 : thisTransform.localPosition.z);
  137. if (rotation) {
  138. if (bone.Data.Inherit.InheritsRotation()) {
  139. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation);
  140. } else {
  141. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  142. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  143. }
  144. }
  145. if (scale) {
  146. thisTransform.localScale = new Vector3(bone.AScaleX, bone.AScaleY, 1f);
  147. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  148. }
  149. break;
  150. }
  151. } else if (mode == Mode.Override) {
  152. if (transformLerpComplete)
  153. return;
  154. if (parentReference == null) {
  155. if (position) {
  156. Vector3 clp = thisTransform.localPosition / positionScale;
  157. bone.X = Mathf.Lerp(bone.X, clp.x, overrideAlpha);
  158. bone.Y = Mathf.Lerp(bone.Y, clp.y, overrideAlpha);
  159. }
  160. if (rotation) {
  161. float angle = Mathf.LerpAngle(bone.Rotation, thisTransform.localRotation.eulerAngles.z, overrideAlpha);
  162. bone.Rotation = angle;
  163. bone.AppliedRotation = angle;
  164. }
  165. if (scale) {
  166. Vector3 cls = thisTransform.localScale;
  167. bone.ScaleX = Mathf.Lerp(bone.ScaleX, cls.x, overrideAlpha);
  168. bone.ScaleY = Mathf.Lerp(bone.ScaleY, cls.y, overrideAlpha);
  169. }
  170. } else {
  171. if (transformLerpComplete)
  172. return;
  173. if (position) {
  174. Vector3 pos = parentReference.InverseTransformPoint(thisTransform.position) / positionScale;
  175. bone.X = Mathf.Lerp(bone.X, pos.x, overrideAlpha);
  176. bone.Y = Mathf.Lerp(bone.Y, pos.y, overrideAlpha);
  177. }
  178. if (rotation) {
  179. float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(thisTransform.up)).eulerAngles.z, overrideAlpha);
  180. bone.Rotation = angle;
  181. bone.AppliedRotation = angle;
  182. }
  183. if (scale) {
  184. Vector3 cls = thisTransform.localScale;
  185. bone.ScaleX = Mathf.Lerp(bone.ScaleX, cls.x, overrideAlpha);
  186. bone.ScaleY = Mathf.Lerp(bone.ScaleY, cls.y, overrideAlpha);
  187. }
  188. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  189. }
  190. transformLerpComplete = true;
  191. }
  192. }
  193. public static bool BoneTransformModeIncompatible (Bone bone) {
  194. return !bone.Data.Inherit.InheritsScale();
  195. }
  196. public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
  197. SkeletonUtility.AddBoneRigidbody2D(transform.gameObject);
  198. SkeletonUtility.AddBoundingBoxGameObject(bone.Skeleton, skinName, slotName, attachmentName, transform);
  199. }
  200. #if UNITY_EDITOR
  201. void OnDrawGizmos () {
  202. if (IncompatibleTransformMode)
  203. Gizmos.DrawIcon(transform.position + new Vector3(0, 0.128f, 0), "icon-warning");
  204. }
  205. #endif
  206. }
  207. }