ManaAudio.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ManaAudio : Regist
  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. Transform tra = new GameObject("Audio").transform;
  14. ManaReso.Get("MusicMini", Folder.Object, true, tra, true);
  15. ManaReso.Get("MusicTheme", Folder.Object, true, tra, true);
  16. }
  17. public override void RegistValueA()
  18. {
  19. #region MusicMini
  20. Transform tra = MusicMini.transform;
  21. TweenAudio tween = tra.CreateTweenAudio(1, 2f, true, true, Curve.EaseOutQuad);
  22. tween.OnForwardStart += () =>
  23. {
  24. MusicMini.Play();
  25. CoroTheme = StartCoroutine(Loop(MusicMini));
  26. };
  27. tween.OnBackwardStart += () =>
  28. {
  29. StopCoroutine(CoroTheme);
  30. };
  31. #endregion
  32. #region MusicTheme
  33. tra = MusicTheme.transform;
  34. tween = tra.CreateTweenAudio(1, 2f, true, true, Curve.EaseOutQuad);
  35. tween.OnForwardStart += () =>
  36. {
  37. MusicTheme.Play();
  38. CoroTheme = StartCoroutine(Loop(MusicTheme));
  39. };
  40. tween.OnBackwardStart += () =>
  41. {
  42. StopCoroutine(CoroTheme);
  43. };
  44. #endregion
  45. ManaReso.Get("MusicTheme").TweenForAudio();
  46. }
  47. public override void RegistReference()
  48. {
  49. MusicMini = ManaReso.Get<AudioSource>("MusicMini");
  50. MusicTheme = ManaReso.Get<AudioSource>("MusicTheme");
  51. }
  52. private IEnumerator Loop(AudioSource audioSource)
  53. {
  54. while (true)
  55. {
  56. while (audioSource.isPlaying)
  57. {
  58. yield return null;
  59. }
  60. yield return new WaitForSeconds(3);
  61. audioSource.Play();
  62. }
  63. }
  64. }