源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 rivejä
7.1 KiB

1 kuukausi sitten
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using UnityEngine;
  32. namespace Spine.Unity {
  33. /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
  34. [ExecuteInEditMode]
  35. [AddComponentMenu("Spine/BoneFollower")]
  36. public class BoneFollower : MonoBehaviour {
  37. #region Inspector
  38. public SkeletonRenderer skeletonRenderer;
  39. public SkeletonRenderer SkeletonRenderer {
  40. get { return skeletonRenderer; }
  41. set {
  42. skeletonRenderer = value;
  43. Initialize();
  44. }
  45. }
  46. /// <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>
  47. [SpineBone(dataField: "skeletonRenderer")]
  48. [SerializeField] public string boneName;
  49. public bool followZPosition = true;
  50. public bool followBoneRotation = true;
  51. [Tooltip("Follows the skeleton's flip state by controlling this Transform's local scale.")]
  52. public bool followSkeletonFlip = true;
  53. [Tooltip("Follows the target bone's local scale. BoneFollower cannot inherit world/skewed scale because of UnityEngine.Transform property limitations.")]
  54. public bool followLocalScale = false;
  55. [UnityEngine.Serialization.FormerlySerializedAs("resetOnAwake")]
  56. public bool initializeOnAwake = true;
  57. #endregion
  58. [NonSerialized] public bool valid;
  59. /// <summary>
  60. /// The bone.
  61. /// </summary>
  62. [NonSerialized] public Bone bone;
  63. Transform skeletonTransform;
  64. bool skeletonTransformIsParent;
  65. /// <summary>
  66. /// Sets the target bone by its bone name. Returns false if no bone was found. To set the bone by reference, use BoneFollower.bone directly.</summary>
  67. public bool SetBone (string name) {
  68. bone = skeletonRenderer.skeleton.FindBone(name);
  69. if (bone == null) {
  70. Debug.LogError("Bone not found: " + name, this);
  71. return false;
  72. }
  73. boneName = name;
  74. return true;
  75. }
  76. public void Awake () {
  77. if (initializeOnAwake) Initialize();
  78. }
  79. public void HandleRebuildRenderer (SkeletonRenderer skeletonRenderer) {
  80. Initialize();
  81. }
  82. public void Initialize () {
  83. bone = null;
  84. valid = skeletonRenderer != null && skeletonRenderer.valid;
  85. if (!valid) return;
  86. skeletonTransform = skeletonRenderer.transform;
  87. skeletonRenderer.OnRebuild -= HandleRebuildRenderer;
  88. skeletonRenderer.OnRebuild += HandleRebuildRenderer;
  89. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  90. if (!string.IsNullOrEmpty(boneName))
  91. bone = skeletonRenderer.skeleton.FindBone(boneName);
  92. #if UNITY_EDITOR
  93. if (Application.isEditor)
  94. LateUpdate();
  95. #endif
  96. }
  97. void OnDestroy () {
  98. if (skeletonRenderer != null)
  99. skeletonRenderer.OnRebuild -= HandleRebuildRenderer;
  100. }
  101. public void LateUpdate () {
  102. if (!valid) {
  103. Initialize();
  104. return;
  105. }
  106. #if UNITY_EDITOR
  107. if (!Application.isPlaying)
  108. skeletonTransformIsParent = Transform.ReferenceEquals(skeletonTransform, transform.parent);
  109. #endif
  110. if (bone == null) {
  111. if (string.IsNullOrEmpty(boneName)) return;
  112. bone = skeletonRenderer.skeleton.FindBone(boneName);
  113. if (!SetBone(boneName)) return;
  114. }
  115. Transform thisTransform = this.transform;
  116. if (skeletonTransformIsParent) {
  117. // Recommended setup: Use local transform properties if Spine GameObject is the immediate parent
  118. thisTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : thisTransform.localPosition.z);
  119. if (followBoneRotation) {
  120. float halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f;
  121. if (followLocalScale && bone.scaleX < 0) // Negate rotation from negative scaleX. Don't use negative determinant. local scaleY doesn't factor into used rotation.
  122. halfRotation += Mathf.PI * 0.5f;
  123. var q = default(Quaternion);
  124. q.z = Mathf.Sin(halfRotation);
  125. q.w = Mathf.Cos(halfRotation);
  126. thisTransform.localRotation = q;
  127. }
  128. } else {
  129. // For special cases: Use transform world properties if transform relationship is complicated
  130. Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f));
  131. if (!followZPosition) targetWorldPosition.z = thisTransform.position.z;
  132. float boneWorldRotation = bone.WorldRotationX;
  133. Transform transformParent = thisTransform.parent;
  134. if (transformParent != null) {
  135. Matrix4x4 m = transformParent.localToWorldMatrix;
  136. if (m.m00 * m.m11 - m.m01 * m.m10 < 0) // Determinant2D is negative
  137. boneWorldRotation = -boneWorldRotation;
  138. }
  139. if (followBoneRotation) {
  140. Vector3 worldRotation = skeletonTransform.rotation.eulerAngles;
  141. if (followLocalScale && bone.scaleX < 0) boneWorldRotation += 180f;
  142. #if UNITY_5_6_OR_NEWER
  143. thisTransform.SetPositionAndRotation(targetWorldPosition, Quaternion.Euler(worldRotation.x, worldRotation.y, worldRotation.z + boneWorldRotation));
  144. #else
  145. thisTransform.position = targetWorldPosition;
  146. thisTransform.rotation = Quaternion.Euler(worldRotation.x, worldRotation.y, worldRotation.z + bone.WorldRotationX);
  147. #endif
  148. } else {
  149. thisTransform.position = targetWorldPosition;
  150. }
  151. }
  152. Vector3 localScale = followLocalScale ? new Vector3(bone.scaleX, bone.scaleY, 1f) : new Vector3(1f, 1f, 1f);
  153. if (followSkeletonFlip) localScale.y *= bone.skeleton.flipX ^ bone.skeleton.flipY ? -1f : 1f;
  154. thisTransform.localScale = localScale;
  155. }
  156. }
  157. }