BoneFollowerGraphic.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.")]
  60. public bool followLocalScale = false;
  61. [Tooltip("Includes the parent bone's lossy world scale. BoneFollower cannot inherit rotated/skewed scale because of UnityEngine.Transform property limitations.")]
  62. public bool followParentWorldScale = false;
  63. public bool followXYPosition = true;
  64. public bool followZPosition = true;
  65. [Tooltip("Applies when 'Follow Skeleton Flip' is disabled but 'Follow Bone Rotation' is enabled."
  66. + " When flipping the skeleton by scaling its Transform, this follower's rotation is adjusted"
  67. + " instead of its scale to follow the bone orientation. When one of the axes is flipped, "
  68. + " only one axis can be followed, either the X or the Y axis, which is selected here.")]
  69. public AxisOrientation maintainedAxisOrientation = AxisOrientation.XAxis;
  70. [System.NonSerialized] public Bone bone;
  71. Transform skeletonTransform;
  72. bool skeletonTransformIsParent;
  73. [System.NonSerialized] public bool valid;
  74. /// <summary>
  75. /// Sets the target bone by its bone name. Returns false if no bone was found.</summary>
  76. public bool SetBone (string name) {
  77. bone = skeletonGraphic.Skeleton.FindBone(name);
  78. if (bone == null) {
  79. Debug.LogError("Bone not found: " + name, this);
  80. return false;
  81. }
  82. boneName = name;
  83. return true;
  84. }
  85. public void Awake () {
  86. if (initializeOnAwake) Initialize();
  87. }
  88. public void Initialize () {
  89. bone = null;
  90. valid = skeletonGraphic != null && skeletonGraphic.IsValid;
  91. if (!valid) return;
  92. skeletonTransform = skeletonGraphic.transform;
  93. // skeletonGraphic.OnRebuild -= HandleRebuildRenderer;
  94. // skeletonGraphic.OnRebuild += HandleRebuildRenderer;
  95. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  96. if (!string.IsNullOrEmpty(boneName))
  97. bone = skeletonGraphic.Skeleton.FindBone(boneName);
  98. #if UNITY_EDITOR
  99. if (Application.isEditor) {
  100. LateUpdate();
  101. }
  102. #endif
  103. }
  104. public void LateUpdate () {
  105. if (!valid) {
  106. Initialize();
  107. return;
  108. }
  109. #if UNITY_EDITOR
  110. if (!Application.isPlaying)
  111. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  112. #endif
  113. if (bone == null) {
  114. if (string.IsNullOrEmpty(boneName)) return;
  115. bone = skeletonGraphic.Skeleton.FindBone(boneName);
  116. if (!SetBone(boneName)) return;
  117. }
  118. var thisTransform = this.transform as RectTransform;
  119. if (thisTransform == null) return;
  120. var canvas = skeletonGraphic.canvas;
  121. if (canvas == null) canvas = skeletonGraphic.GetComponentInParent<Canvas>();
  122. float scale = canvas != null ? canvas.referencePixelsPerUnit : 100.0f;
  123. float additionalFlipScale = 1;
  124. if (skeletonTransformIsParent) {
  125. // Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
  126. thisTransform.localPosition = new Vector3(followXYPosition ? bone.WorldX * scale : thisTransform.localPosition.x,
  127. followXYPosition ? bone.WorldY * scale : thisTransform.localPosition.y,
  128. followZPosition ? 0f : thisTransform.localPosition.z);
  129. if (followBoneRotation) thisTransform.localRotation = bone.GetQuaternion();
  130. } else {
  131. // For special cases: Use transform world properties if transform relationship is complicated
  132. Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.WorldX * scale, bone.WorldY * scale, 0f));
  133. if (!followZPosition) targetWorldPosition.z = thisTransform.position.z;
  134. if (!followXYPosition) {
  135. targetWorldPosition.x = thisTransform.position.x;
  136. targetWorldPosition.y = thisTransform.position.y;
  137. }
  138. Vector3 skeletonLossyScale = skeletonTransform.lossyScale;
  139. Transform transformParent = thisTransform.parent;
  140. Vector3 parentLossyScale = transformParent != null ? transformParent.lossyScale : Vector3.one;
  141. if (followBoneRotation) {
  142. float boneWorldRotation = bone.WorldRotationX;
  143. if ((skeletonLossyScale.x * skeletonLossyScale.y) < 0)
  144. boneWorldRotation = -boneWorldRotation;
  145. if (followSkeletonFlip || maintainedAxisOrientation == AxisOrientation.XAxis) {
  146. if ((skeletonLossyScale.x * parentLossyScale.x < 0))
  147. boneWorldRotation += 180f;
  148. } else {
  149. if ((skeletonLossyScale.y * parentLossyScale.y < 0))
  150. boneWorldRotation += 180f;
  151. }
  152. Vector3 worldRotation = skeletonTransform.rotation.eulerAngles;
  153. if (followLocalScale && bone.ScaleX < 0) boneWorldRotation += 180f;
  154. thisTransform.SetPositionAndRotation(targetWorldPosition, Quaternion.Euler(worldRotation.x, worldRotation.y, worldRotation.z + boneWorldRotation));
  155. } else {
  156. thisTransform.position = targetWorldPosition;
  157. }
  158. additionalFlipScale = Mathf.Sign(skeletonLossyScale.x * parentLossyScale.x
  159. * skeletonLossyScale.y * parentLossyScale.y);
  160. }
  161. Bone parentBone = bone.Parent;
  162. Vector3 localScale = new Vector3(1f, 1f, 1f);
  163. if (followParentWorldScale && parentBone != null)
  164. localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1f);
  165. if (followLocalScale)
  166. localScale.Scale(new Vector3(bone.ScaleX, bone.ScaleY, 1f));
  167. if (followSkeletonFlip)
  168. localScale.y *= Mathf.Sign(bone.Skeleton.ScaleX * bone.Skeleton.ScaleY) * additionalFlipScale;
  169. thisTransform.localScale = localScale;
  170. }
  171. }
  172. }