AnimatorControl.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AnimatorControl : MonoBehaviour
  5. {
  6. public Animator animator;
  7. [Header("是否显示老师模型")]
  8. public bool modelActive;
  9. [Header("是否播放动画")]
  10. public bool animatonPlay;
  11. [Header("结束时是否显示老师模型")]
  12. public bool modelEndActive = true;
  13. private AudioSource audioSource;
  14. private void Awake()
  15. {
  16. audioSource = GetComponent<AudioSource>();
  17. }
  18. private void OnEnable()
  19. {
  20. animator.gameObject.SetActive(modelActive);
  21. animator.SetBool("S1", false);
  22. animator.SetBool("S2", false);
  23. animator.SetBool("S3", false);
  24. animator.SetBool("Hold", false);
  25. if (animatonPlay)
  26. {
  27. animator.SetBool("S" + Random.Range(1, 4), true);
  28. }
  29. else
  30. {
  31. animator.SetBool("Hold", true);
  32. }
  33. StartCoroutine(Hold());
  34. }
  35. void Start()
  36. {
  37. }
  38. private void OnDisable()
  39. {
  40. audioSource.Stop();
  41. animator.SetBool("S1", false);
  42. animator.SetBool("S2", false);
  43. animator.SetBool("S3", false);
  44. animator.SetBool("Hold", true);
  45. if (!modelEndActive)
  46. {
  47. animator.gameObject.SetActive(false);
  48. }
  49. }
  50. private IEnumerator Hold()
  51. {
  52. yield return new WaitForSeconds(1);
  53. while (audioSource.isPlaying)
  54. {
  55. yield return null;
  56. }
  57. animator.SetBool("S1", false);
  58. animator.SetBool("S2", false);
  59. animator.SetBool("S3", false);
  60. animator.SetBool("Hold", true);
  61. }
  62. }
粤ICP备19079148号