AdvAnimation.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SoftKitty
  5. {
  6. /// <summary>
  7. /// Add this compoent along with the legacy 'Animation' component to make it works when timeScale==0
  8. /// When you call native functions of legacy 'Animation' component, call same functions in this compoent insteaded. For example: GetComponent<AdvAnimation>().Play();
  9. /// </summary>
  10. public class AdvAnimation : MonoBehaviour
  11. {
  12. private Animation ani
  13. {
  14. get
  15. {
  16. if (_ani == null) _ani = GetComponent<Animation>();
  17. return _ani;
  18. }
  19. }
  20. private Animation _ani;
  21. private string currentAnimation = "";
  22. public bool isPlaying
  23. {
  24. get
  25. {
  26. if (ani != null)
  27. return ani.isPlaying;
  28. else
  29. return false;
  30. }
  31. }
  32. public AnimationClip clip
  33. {
  34. get
  35. {
  36. if (ani != null)
  37. return ani.clip;
  38. else
  39. return null;
  40. }
  41. }
  42. public int GetClipCount()
  43. {
  44. if (ani != null)
  45. return ani.GetClipCount();
  46. else
  47. return 0;
  48. }
  49. public bool Play()
  50. {
  51. if (ani == null) return false;
  52. currentAnimation = ani.clip.name;
  53. return ani.Play();
  54. }
  55. public bool Play(string animation)
  56. {
  57. if (ani == null) return false;
  58. currentAnimation = animation;
  59. return ani.Play(animation);
  60. }
  61. public void Stop()
  62. {
  63. if (ani == null) return;
  64. ani.Stop();
  65. if (ani.clip != null) currentAnimation = ani.clip.name;
  66. }
  67. public bool IsPlaying(string name)
  68. {
  69. if (ani == null) return false;
  70. return ani.IsPlaying(name);
  71. }
  72. private void Start()
  73. {
  74. if (ani == null) return;
  75. if (ani.clip!=null)currentAnimation = ani.clip.name;
  76. }
  77. private void Update()
  78. {
  79. if (Time.timeScale>0F || !ani.isPlaying || string.IsNullOrEmpty(currentAnimation)) return;
  80. AnimationState currentState = ani[currentAnimation];
  81. if (currentState.time < currentState.length)
  82. {
  83. currentState.time += Time.unscaledDeltaTime;
  84. ani.Sample();
  85. }
  86. }
  87. }
  88. }