SkeletonAnimation.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using System.IO;
  32. using System.Collections.Generic;
  33. using UnityEngine;
  34. using Spine;
  35. [ExecuteInEditMode]
  36. [AddComponentMenu("Spine/SkeletonAnimation")]
  37. public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation
  38. {
  39. public float timeScale = 1;
  40. public bool loop;
  41. public Spine.AnimationState state;
  42. public event UpdateBonesDelegate UpdateLocal
  43. {
  44. add { _UpdateLocal += value; }
  45. remove { _UpdateLocal -= value; }
  46. }
  47. public event UpdateBonesDelegate UpdateWorld
  48. {
  49. add { _UpdateWorld += value; }
  50. remove { _UpdateWorld -= value; }
  51. }
  52. public event UpdateBonesDelegate UpdateComplete
  53. {
  54. add { _UpdateComplete += value; }
  55. remove { _UpdateComplete -= value; }
  56. }
  57. protected event UpdateBonesDelegate _UpdateLocal;
  58. protected event UpdateBonesDelegate _UpdateWorld;
  59. protected event UpdateBonesDelegate _UpdateComplete;
  60. [SerializeField]
  61. private String
  62. _animationName;
  63. public String AnimationName
  64. {
  65. get
  66. {
  67. TrackEntry entry = state.GetCurrent(0);
  68. return entry == null ? null : entry.Animation.Name;
  69. }
  70. set
  71. {
  72. if (_animationName == value)
  73. return;
  74. _animationName = value;
  75. if (value == null || value.Length == 0)
  76. state.ClearTrack(0);
  77. else
  78. state.SetAnimation(0, value, loop);
  79. }
  80. }
  81. public override void Reset()
  82. {
  83. base.Reset();
  84. if (!valid)
  85. return;
  86. state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
  87. if (_animationName != null && _animationName.Length > 0)
  88. {
  89. state.SetAnimation(0, _animationName, loop);
  90. Update(0);
  91. }
  92. }
  93. public virtual void Update()
  94. {
  95. Update(Time.deltaTime);
  96. }
  97. public virtual void Update(float deltaTime)
  98. {
  99. if (!valid)
  100. return;
  101. deltaTime *= timeScale;
  102. skeleton.Update(deltaTime);
  103. state.Update(deltaTime);
  104. state.Apply(skeleton);
  105. if (_UpdateLocal != null)
  106. _UpdateLocal(this);
  107. skeleton.UpdateWorldTransform();
  108. if (_UpdateWorld != null)
  109. {
  110. _UpdateWorld(this);
  111. skeleton.UpdateWorldTransform();
  112. }
  113. if (_UpdateComplete != null)
  114. {
  115. _UpdateComplete(this);
  116. }
  117. }
  118. }