BoneLocalOverride.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Spine;
  30. using Spine.Unity;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class BoneLocalOverride : MonoBehaviour {
  34. [SpineBone]
  35. public string boneName;
  36. [Space]
  37. [Range(0, 1)] public float alpha = 1;
  38. [Space]
  39. public bool overridePosition = true;
  40. public Vector2 localPosition;
  41. [Space]
  42. public bool overrideRotation = true;
  43. [Range(0, 360)] public float rotation = 0;
  44. ISkeletonAnimation spineComponent;
  45. Bone bone;
  46. #if UNITY_EDITOR
  47. void OnValidate () {
  48. if (Application.isPlaying) return;
  49. if (spineComponent == null) spineComponent = GetComponent<ISkeletonAnimation>();
  50. if (spineComponent.IsNullOrDestroyed()) return;
  51. if (bone != null) bone.SetToSetupPose();
  52. OverrideLocal(spineComponent);
  53. }
  54. #endif
  55. void Awake () {
  56. spineComponent = GetComponent<ISkeletonAnimation>();
  57. if (spineComponent == null) { this.enabled = false; return; }
  58. spineComponent.UpdateLocal += OverrideLocal;
  59. if (bone == null) { this.enabled = false; return; }
  60. }
  61. void OverrideLocal (ISkeletonAnimation animated) {
  62. if (bone == null || bone.Data.Name != boneName) {
  63. if (string.IsNullOrEmpty(boneName)) return;
  64. bone = spineComponent.Skeleton.FindBone(boneName);
  65. if (bone == null) {
  66. Debug.LogFormat("Cannot find bone: '{0}'", boneName);
  67. return;
  68. }
  69. }
  70. if (overridePosition) {
  71. bone.X = Mathf.Lerp(bone.X, localPosition.x, alpha);
  72. bone.Y = Mathf.Lerp(bone.Y, localPosition.y, alpha);
  73. }
  74. if (overrideRotation)
  75. bone.Rotation = Mathf.Lerp(bone.Rotation, rotation, alpha);
  76. }
  77. }
  78. }