ManaAudio.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ManaAudio : MonoBehaviour
  4. {
  5. #region 变量
  6. public Coroutine CoroTheme;
  7. public Coroutine CoroMiniGame;
  8. public AudioSource MusicMini;
  9. public AudioSource MusicTheme;
  10. #endregion
  11. private void Awake()
  12. {
  13. Initializer.RegistValue += RegistValue;
  14. Initializer.RegistReference+= RegistReference;
  15. ManaReso.Get("MusicMini", Folder.Object, true, null, true);
  16. ManaReso.Get("MusicTheme", Folder.Object, true, null, true);
  17. }
  18. private void RegistValue()
  19. {
  20. #region MusicMini
  21. Transform tra = MusicMini.transform;
  22. TweenAudio tween = tra.CreateTweenAudio(1, 2f, true, true, Curve.EaseOutQuad);
  23. tween.OnForwardStart += () =>
  24. {
  25. MusicMini.Play();
  26. CoroTheme = StartCoroutine(Loop(MusicMini));
  27. };
  28. tween.OnBackwardStart += () =>
  29. {
  30. StopCoroutine(CoroTheme);
  31. };
  32. #endregion
  33. #region MusicTheme
  34. tra = MusicTheme.transform;
  35. tween = tra.CreateTweenAudio(1, 2f, true, true, Curve.EaseOutQuad);
  36. tween.OnForwardStart += () =>
  37. {
  38. MusicTheme.Play();
  39. CoroTheme = StartCoroutine(Loop(MusicTheme));
  40. };
  41. tween.OnBackwardStart += () =>
  42. {
  43. StopCoroutine(CoroTheme);
  44. };
  45. #endregion
  46. ManaReso.Get("MusicTheme").TweenForAudio();
  47. }
  48. private void RegistReference()
  49. {
  50. MusicMini = ManaReso.Get<AudioSource>("MusicMini");
  51. MusicTheme = ManaReso.Get<AudioSource>("MusicTheme");
  52. }
  53. private IEnumerator Loop(AudioSource audioSource)
  54. {
  55. while (true)
  56. {
  57. while (audioSource.isPlaying)
  58. {
  59. yield return null;
  60. }
  61. yield return new WaitForSeconds(3);
  62. audioSource.Play();
  63. }
  64. }
  65. }