BoneFollowerGraphic.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. using AxisOrientation = BoneFollower.AxisOrientation;
  35. #if NEW_PREFAB_SYSTEM
  36. [ExecuteAlways]
  37. #else
  38. [ExecuteInEditMode]
  39. #endif
  40. [RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
  41. [AddComponentMenu("Spine/UI/BoneFollowerGraphic")]
  42. [HelpURL("http://esotericsoftware.com/spine-unity#BoneFollowerGraphic")]
  43. public class BoneFollowerGraphic : MonoBehaviour {
  44. public SkeletonGraphic skeletonGraphic;
  45. public SkeletonGraphic SkeletonGraphic {
  46. get { return skeletonGraphic; }
  47. set {
  48. skeletonGraphic = value;
  49. Initialize();
  50. }
  51. }
  52. public bool initializeOnAwake = true;
  53. /// <summary>If a bone isn't set in code, boneName is used to find the bone at the beginning. For runtime switching by name, use SetBoneByName. You can also set the BoneFollower.bone field directly.</summary>
  54. [SpineBone(dataField: "skeletonGraphic")]
  55. public string boneName;
  56. public bool followBoneRotation = true;
  57. [Tooltip("Follows the skeleton's flip state by controlling this Transform's local scale.")]
  58. public bool followSkeletonFlip = true;
  59. [Tooltip("Follows the target bone's local scale. BoneFollower cannot inherit world/skewed scale because of UnityEngine.Transform property limitations.")]
  60. public bool followLocalScale = false;
  61. public bool followXYPosition = true;
  62. public bool followZPosition = true;
  63. [Tooltip("Applies when 'Follow Skeleton Flip' is disabled but 'Follow Bone Rotation' is enabled."
  64. + " When flipping the skeleton by scaling its Transform, this follower's rotation is adjusted"
  65. + " instead of its scale to follow the bone orientation. When one of the axes is flipped, "
  66. + " only one axis can be followed, either the X or the Y axis, which is selected here.")]
  67. public AxisOrientation maintainedAxisOrientation = AxisOrientation.XAxis;
  68. [System.NonSerialized] public Bone bone;
  69. Transform skeletonTransform;
  70. bool skeletonTransformIsParent;
  71. [System.NonSerialized] public bool valid;
  72. /// <summary>
  73. /// Sets the target bone by its bone name. Returns false if no bone was found.</summary>
  74. public bool SetBone (string name) {
  75. bone = skeletonGraphic.Skeleton.FindBone(name);
  76. if (bone == null) {
  77. Debug.LogError("Bone not found: " + name, this);
  78. return false;
  79. }
  80. boneName = name;
  81. return true;
  82. }
  83. public void Awake () {
  84. if (initializeOnAwake) Initialize();
  85. }
  86. public void Initialize () {
  87. bone = null;
  88. valid = skeletonGraphic != null && skeletonGraphic.IsValid;
  89. if (!valid) return;
  90. skeletonTransform = skeletonGraphic.transform;
  91. // skeletonGraphic.OnRebuild -= HandleRebuildRenderer;
  92. // skeletonGraphic.OnRebuild += HandleRebuildRenderer;
  93. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  94. if (!string.IsNullOrEmpty(boneName))
  95. bone = skeletonGraphic.Skeleton.FindBone(boneName);
  96. #if UNITY_EDITOR
  97. if (Application.isEditor) {
  98. LateUpdate();
  99. }
  100. #endif
  101. }
  102. public void LateUpdate () {
  103. if (!valid) {
  104. Initialize();
  105. return;
  106. }
  107. #if UNITY_EDITOR
  108. if (!Application.isPlaying)
  109. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  110. #endif
  111. if (bone == null) {
  112. if (string.IsNullOrEmpty(boneName)) return;
  113. bone = skeletonGraphic.Skeleton.FindBone(boneName);
  114. if (!SetBone(boneName)) return;
  115. }
  116. var thisTransform = this.transform as RectTransform;
  117. if (thisTransform == null) return;
  118. var canvas = skeletonGraphic.canvas;
  119. if (canvas == null) canvas = skeletonGraphic.GetComponentInParent<Canvas>();
  120. float scale = canvas != null ? canvas.referencePixelsPerUnit : 100.0f;
  121. float additionalFlipScale = 1;
  122. if (skeletonTransformIsParent) {
  123. // Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
  124. thisTransform.localPosition = new Vector3(followXYPosition ? bone.worldX * scale : thisTransform.localPosition.x,
  125. followXYPosition ? bone.worldY * scale : thisTransform.localPosition.y,
  126. followZPosition ? 0f : thisTransform.localPosition.z);
  127. if (followBoneRotation) thisTransform.localRotation = bone.GetQuaternion();
  128. } else {
  129. // For special cases: Use transform world properties if transform relationship is complicated
  130. Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX * scale, bone.worldY * scale, 0f));
  131. if (!followZPosition) targetWorldPosition.z = thisTransform.position.z;
  132. if (!followXYPosition) {
  133. targetWorldPosition.x = thisTransform.position.x;
  134. targetWorldPosition.y = thisTransform.position.y;
  135. }
  136. Vector3 skeletonLossyScale = skeletonTransform.lossyScale;
  137. Transform transformParent = thisTransform.parent;
  138. Vector3 parentLossyScale = transformParent != null ? transformParent.lossyScale : Vector3.one;
  139. if (followBoneRotation) {
  140. float boneWorldRotation = bone.WorldRotationX;
  141. if ((skeletonLossyScale.x * skeletonLossyScale.y) < 0)
  142. boneWorldRotation = -boneWorldRotation;
  143. if (followSkeletonFlip || maintainedAxisOrientation == AxisOrientation.XAxis) {
  144. if ((skeletonLossyScale.x * parentLossyScale.x < 0))
  145. boneWorldRotation += 180f;
  146. }
  147. else {
  148. if ((skeletonLossyScale.y * parentLossyScale.y < 0))
  149. boneWorldRotation += 180f;
  150. }
  151. Vector3 worldRotation = skeletonTransform.rotation.eulerAngles;
  152. if (followLocalScale && bone.scaleX < 0) boneWorldRotation += 180f;
  153. thisTransform.SetPositionAndRotation(targetWorldPosition, Quaternion.Euler(worldRotation.x, worldRotation.y, worldRotation.z + boneWorldRotation));
  154. } else {
  155. thisTransform.position = targetWorldPosition;
  156. }
  157. additionalFlipScale = Mathf.Sign(skeletonLossyScale.x * parentLossyScale.x
  158. * skeletonLossyScale.y * parentLossyScale.y);
  159. }
  160. Vector3 localScale = followLocalScale ? new Vector3(bone.scaleX, bone.scaleY, 1f) : new Vector3(1f, 1f, 1f);
  161. if (followSkeletonFlip)
  162. localScale.y *= Mathf.Sign(bone.skeleton.ScaleX * bone.skeleton.ScaleY) * additionalFlipScale;
  163. thisTransform.localScale = localScale;
  164. }
  165. }
  166. }