AnimationPlayer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. /* 自定义动画播放器 */
  3. public class AnimationPlayer : MonoBehaviour
  4. {
  5. public float speed = 1.0f;
  6. Animation animationMain;
  7. AnimationClip[] _animationClips;
  8. AnimationClip[] animationClips
  9. {
  10. get {
  11. if (this.animationMain == null) {
  12. this.animationMain = this.GetComponent<Animation>();
  13. }
  14. if (this._animationClips == null) {
  15. this._animationClips = new AnimationClip[this.animationMain.GetClipCount()];
  16. int i = 0;
  17. foreach (AnimationState AS in this.animationMain) {
  18. AS.speed = this.speed;
  19. this._animationClips[i] = this.animationMain.GetClip(AS.name);
  20. i++;
  21. }
  22. }
  23. return this._animationClips;
  24. }
  25. }
  26. WrapMode _wrapMode = WrapMode.Default;
  27. int _index = 0;
  28. bool _playing = false;
  29. float _playingTime = 0;
  30. float _animationTime = 0;
  31. string _animationName = "";
  32. int _completeCount = 0;
  33. public System.Action<AnimationPlayerCompleteResult> completeCallback;
  34. public void play(int index, WrapMode wrapMode = WrapMode.Default) {
  35. Resume();
  36. this._wrapMode = wrapMode;
  37. this._index = index;
  38. this._playing = true;
  39. this._playingTime = 0;
  40. this._completeCount = 0;
  41. this._animationTime = this.animationClips[index].length / this.speed;
  42. this._animationName = this.animationClips[index].name;
  43. this.animationMain[this._animationName].wrapMode = wrapMode;
  44. this.animationMain.CrossFade(this._animationName);
  45. }
  46. void Update()
  47. {
  48. if (this._playing && !_pausing) {
  49. this._playingTime += Time.deltaTime;
  50. int completeCount = (int) (this._playingTime / this._animationTime);
  51. if (completeCount > this._completeCount) {
  52. this._completeCount++;
  53. if (this._wrapMode != WrapMode.Loop) {
  54. this._playing = false;
  55. }
  56. if (this.completeCallback != null) {
  57. AnimationPlayerCompleteResult res = new AnimationPlayerCompleteResult();
  58. res.index = this._index;
  59. res.animationName = this._animationName;
  60. res.completeCount = this._completeCount;
  61. completeCallback(res);
  62. }
  63. }
  64. }
  65. }
  66. private float animationPauseTime;
  67. private bool _pausing = false;
  68. //暂停
  69. public void Pause()
  70. {
  71. if (_pausing) return;
  72. _pausing = true;
  73. if (_playing) {
  74. _playing = false;
  75. AnimationState aniState = animationMain[_animationName];
  76. if (aniState) {
  77. animationPauseTime = aniState.time;
  78. animationMain.Stop(_animationName);
  79. }
  80. }
  81. }
  82. //恢复
  83. public void Resume()
  84. {
  85. if (_pausing) {
  86. _pausing = false;
  87. _playing = true;
  88. AnimationState aniState = animationMain[_animationName];
  89. if (aniState) {
  90. aniState.time = animationPauseTime;
  91. animationMain.Play(_animationName);
  92. }
  93. }
  94. }
  95. }
  96. public class AnimationPlayerCompleteResult {
  97. public int index = 0;
  98. public string animationName = "";
  99. public int completeCount = 0;
  100. }