SkeletonUtilityKinematicShadow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. using System.Collections.Generic;
  30. using UnityEngine;
  31. namespace Spine.Unity.Examples {
  32. // SkeletonUtilityKinematicShadow allows hinge chains to inherit a velocity interpreted from changes in parent transform position or from unrelated rigidbodies.
  33. // Note: Uncheck "useRootTransformIfNull
  34. public class SkeletonUtilityKinematicShadow : MonoBehaviour {
  35. #region Inspector
  36. [Tooltip("If checked, the hinge chain can inherit your root transform's velocity or position/rotation changes.")]
  37. public bool detachedShadow = false;
  38. public Transform parent;
  39. public bool hideShadow = true;
  40. public PhysicsSystem physicsSystem = PhysicsSystem.Physics3D;
  41. #endregion
  42. GameObject shadowRoot;
  43. readonly List<TransformPair> shadowTable = new List<TransformPair>();
  44. struct TransformPair {
  45. public Transform dest, src;
  46. }
  47. public enum PhysicsSystem {
  48. Physics2D,
  49. Physics3D
  50. };
  51. void Start () {
  52. // Duplicate this gameObject as the "shadow" with a different parent.
  53. shadowRoot = Instantiate<GameObject>(this.gameObject);
  54. Destroy(shadowRoot.GetComponent<SkeletonUtilityKinematicShadow>());
  55. // Prepare shadow gameObject's properties.
  56. var shadowRootTransform = shadowRoot.transform;
  57. shadowRootTransform.position = transform.position;
  58. shadowRootTransform.rotation = transform.rotation;
  59. Vector3 scaleRef = transform.TransformPoint(Vector3.right);
  60. float scale = Vector3.Distance(transform.position, scaleRef);
  61. shadowRootTransform.localScale = Vector3.one;
  62. if (!detachedShadow) {
  63. // Do not change to null coalescing operator (??). Unity overloads null checks for UnityEngine.Objects but not the ?? operator.
  64. if (parent == null)
  65. shadowRootTransform.parent = transform.root;
  66. else
  67. shadowRootTransform.parent = parent;
  68. }
  69. if (hideShadow)
  70. shadowRoot.hideFlags = HideFlags.HideInHierarchy;
  71. var shadowJoints = shadowRoot.GetComponentsInChildren<Joint>();
  72. foreach (Joint j in shadowJoints)
  73. j.connectedAnchor *= scale;
  74. // Build list of bone pairs (matches shadow transforms with bone transforms)
  75. var bones = GetComponentsInChildren<SkeletonUtilityBone>();
  76. var shadowBones = shadowRoot.GetComponentsInChildren<SkeletonUtilityBone>();
  77. foreach (var b in bones) {
  78. if (b.gameObject == this.gameObject)
  79. continue;
  80. System.Type checkType = (physicsSystem == PhysicsSystem.Physics2D) ? typeof(Rigidbody2D) : typeof(Rigidbody);
  81. foreach (var sb in shadowBones) {
  82. if (sb.GetComponent(checkType) != null && sb.boneName == b.boneName) {
  83. shadowTable.Add(new TransformPair {
  84. dest = b.transform,
  85. src = sb.transform
  86. });
  87. break;
  88. }
  89. }
  90. }
  91. // Destroy conflicting and unneeded components
  92. DestroyComponents(shadowBones);
  93. DestroyComponents(GetComponentsInChildren<Joint>());
  94. DestroyComponents(GetComponentsInChildren<Rigidbody>());
  95. DestroyComponents(GetComponentsInChildren<Collider>());
  96. }
  97. static void DestroyComponents (Component[] components) {
  98. for (int i = 0, n = components.Length; i < n; i++)
  99. Destroy(components[i]);
  100. }
  101. void FixedUpdate () {
  102. if (physicsSystem == PhysicsSystem.Physics2D) {
  103. var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody2D>();
  104. shadowRootRigidbody.MovePosition(transform.position);
  105. shadowRootRigidbody.MoveRotation(transform.rotation.eulerAngles.z);
  106. } else {
  107. var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody>();
  108. shadowRootRigidbody.MovePosition(transform.position);
  109. shadowRootRigidbody.MoveRotation(transform.rotation);
  110. }
  111. for (int i = 0, n = shadowTable.Count; i < n; i++) {
  112. var pair = shadowTable[i];
  113. pair.dest.localPosition = pair.src.localPosition;
  114. pair.dest.localRotation = pair.src.localRotation;
  115. }
  116. }
  117. }
  118. }