PointFollower.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #if NEW_PREFAB_SYSTEM
  35. [ExecuteAlways]
  36. #else
  37. [ExecuteInEditMode]
  38. #endif
  39. [AddComponentMenu("Spine/Point Follower")]
  40. [HelpURL("http://esotericsoftware.com/spine-unity#PointFollower")]
  41. public class PointFollower : MonoBehaviour, IHasSkeletonRenderer, IHasSkeletonComponent {
  42. public SkeletonRenderer skeletonRenderer;
  43. public SkeletonRenderer SkeletonRenderer { get { return this.skeletonRenderer; } }
  44. public ISkeletonComponent SkeletonComponent { get { return skeletonRenderer as ISkeletonComponent; } }
  45. [SpineSlot(dataField:"skeletonRenderer", includeNone: true)]
  46. public string slotName;
  47. [SpineAttachment(slotField:"slotName", dataField: "skeletonRenderer", fallbackToTextField:true, includeNone: true)]
  48. public string pointAttachmentName;
  49. public bool followRotation = true;
  50. public bool followSkeletonFlip = true;
  51. public bool followSkeletonZPosition = false;
  52. Transform skeletonTransform;
  53. bool skeletonTransformIsParent;
  54. PointAttachment point;
  55. Bone bone;
  56. bool valid;
  57. public bool IsValid { get { return valid; } }
  58. public void Initialize () {
  59. valid = skeletonRenderer != null && skeletonRenderer.valid;
  60. if (!valid)
  61. return;
  62. UpdateReferences();
  63. #if UNITY_EDITOR
  64. if (Application.isEditor) LateUpdate();
  65. #endif
  66. }
  67. private void HandleRebuildRenderer (SkeletonRenderer skeletonRenderer) {
  68. Initialize();
  69. }
  70. void UpdateReferences () {
  71. skeletonTransform = skeletonRenderer.transform;
  72. skeletonRenderer.OnRebuild -= HandleRebuildRenderer;
  73. skeletonRenderer.OnRebuild += HandleRebuildRenderer;
  74. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  75. bone = null;
  76. point = null;
  77. if (!string.IsNullOrEmpty(pointAttachmentName)) {
  78. var skeleton = skeletonRenderer.Skeleton;
  79. int slotIndex = skeleton.FindSlotIndex(slotName);
  80. if (slotIndex >= 0) {
  81. var slot = skeleton.slots.Items[slotIndex];
  82. bone = slot.bone;
  83. point = skeleton.GetAttachment(slotIndex, pointAttachmentName) as PointAttachment;
  84. }
  85. }
  86. }
  87. void OnDestroy () {
  88. if (skeletonRenderer != null)
  89. skeletonRenderer.OnRebuild -= HandleRebuildRenderer;
  90. }
  91. public void LateUpdate () {
  92. #if UNITY_EDITOR
  93. if (!Application.isPlaying) skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  94. #endif
  95. if (point == null) {
  96. if (string.IsNullOrEmpty(pointAttachmentName)) return;
  97. UpdateReferences();
  98. if (point == null) return;
  99. }
  100. Vector2 worldPos;
  101. point.ComputeWorldPosition(bone, out worldPos.x, out worldPos.y);
  102. float rotation = point.ComputeWorldRotation(bone);
  103. Transform thisTransform = this.transform;
  104. if (skeletonTransformIsParent) {
  105. // Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
  106. thisTransform.localPosition = new Vector3(worldPos.x, worldPos.y, followSkeletonZPosition ? 0f : thisTransform.localPosition.z);
  107. if (followRotation) {
  108. float halfRotation = rotation * 0.5f * Mathf.Deg2Rad;
  109. var q = default(Quaternion);
  110. q.z = Mathf.Sin(halfRotation);
  111. q.w = Mathf.Cos(halfRotation);
  112. thisTransform.localRotation = q;
  113. }
  114. } else {
  115. // For special cases: Use transform world properties if transform relationship is complicated
  116. Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(worldPos.x, worldPos.y, 0f));
  117. if (!followSkeletonZPosition)
  118. targetWorldPosition.z = thisTransform.position.z;
  119. Transform transformParent = thisTransform.parent;
  120. if (transformParent != null) {
  121. Matrix4x4 m = transformParent.localToWorldMatrix;
  122. if (m.m00 * m.m11 - m.m01 * m.m10 < 0) // Determinant2D is negative
  123. rotation = -rotation;
  124. }
  125. if (followRotation) {
  126. Vector3 transformWorldRotation = skeletonTransform.rotation.eulerAngles;
  127. thisTransform.SetPositionAndRotation(targetWorldPosition, Quaternion.Euler(transformWorldRotation.x, transformWorldRotation.y, transformWorldRotation.z + rotation));
  128. } else {
  129. thisTransform.position = targetWorldPosition;
  130. }
  131. }
  132. if (followSkeletonFlip) {
  133. Vector3 localScale = thisTransform.localScale;
  134. localScale.y = Mathf.Abs(localScale.y) * Mathf.Sign(bone.skeleton.ScaleX * bone.skeleton.ScaleY);
  135. thisTransform.localScale = localScale;
  136. }
  137. }
  138. }
  139. }