AnimationPlayer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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[this._animationName].speed = this.speed;
  45. this.animationMain.CrossFade(this._animationName);
  46. }
  47. void Update()
  48. {
  49. if (this._playing && !_pausing) {
  50. this._playingTime += Time.deltaTime;
  51. int completeCount = (int) (this._playingTime / this._animationTime);
  52. if (completeCount > this._completeCount) {
  53. this._completeCount++;
  54. if (this._wrapMode != WrapMode.Loop) {
  55. this._playing = false;
  56. }
  57. if (this.completeCallback != null) {
  58. AnimationPlayerCompleteResult res = new AnimationPlayerCompleteResult();
  59. res.index = this._index;
  60. res.animationName = this._animationName;
  61. res.completeCount = this._completeCount;
  62. completeCallback(res);
  63. }
  64. }
  65. }
  66. }
  67. private float animationPauseTime;
  68. private bool _pausing = false;
  69. //暂停
  70. public void Pause()
  71. {
  72. if (_pausing) return;
  73. _pausing = true;
  74. if (_playing) {
  75. _playing = false;
  76. AnimationState aniState = animationMain[_animationName];
  77. if (aniState) {
  78. animationPauseTime = aniState.time;
  79. animationMain.Stop(_animationName);
  80. }
  81. }
  82. }
  83. //恢复
  84. public void Resume()
  85. {
  86. if (_pausing) {
  87. _pausing = false;
  88. _playing = true;
  89. AnimationState aniState = animationMain[_animationName];
  90. if (aniState) {
  91. aniState.time = animationPauseTime;
  92. animationMain.Play(_animationName);
  93. }
  94. }
  95. }
  96. public void StopAnimation(int index) {
  97. string name = this.animationClips[index].name;
  98. animationMain.Stop(name);
  99. }
  100. }
  101. public class AnimationPlayerCompleteResult {
  102. public int index = 0;
  103. public string animationName = "";
  104. public int completeCount = 0;
  105. }