AudioManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AudioManager : MonoBehaviour
  5. {
  6. public static AudioManager Instance { get; private set; }
  7. private AudioSource ad;
  8. private Animator anim;
  9. private Dictionary<string,AudioClip> clipDic = new Dictionary<string,AudioClip>();
  10. private async void Awake()
  11. {
  12. Instance = this;
  13. ad = gameObject.AddComponent<AudioSource>();
  14. anim = gameObject.AddComponent<Animator>();
  15. ad.volume = 0.5f;
  16. //anim.runtimeAnimatorController = await YooAssetManager.Instance.LoadAsset<RuntimeAnimatorController>("AudioManager");
  17. DontDestroyOnLoad(gameObject);
  18. }
  19. /// <summary>
  20. /// 꺄렴교쒼稜있
  21. /// </summary>
  22. /// <param name="audioName"></param>
  23. public async void PlayAudio(string audioName, bool loop = false)
  24. {
  25. if (ad.clip == null)
  26. {
  27. ad.clip = await YooAssetManager.Instance.LoadAsset<AudioClip>(audioName);
  28. ad.volume = 0.5f;
  29. ad.Play();
  30. ad.loop = true;
  31. }
  32. else
  33. {
  34. Debug.Log(anim);
  35. //anim.Play("bgmdown");
  36. StartCoroutine("PlayUp", audioName);
  37. }
  38. }
  39. /// <summary>
  40. /// 못膠竟藤속�稜莉숭 꺄렴뗌접돨稜틉
  41. /// </summary>
  42. /// <param name="audioName"></param>
  43. public void PlayDirectAudio(GameObject go, string audioName, float volume = 1, bool loop = false)
  44. {
  45. AudioSource goAD;
  46. if (go.GetComponent<AudioSource>())
  47. {
  48. goAD = go.GetComponent<AudioSource>();
  49. }
  50. else
  51. {
  52. goAD = go.AddComponent<AudioSource>();
  53. }
  54. goAD.spatialBlend = 0; goAD.dopplerLevel = 1; goAD.spread = 45; goAD.rolloffMode = AudioRolloffMode.Logarithmic;
  55. goAD.minDistance = 20; goAD.maxDistance = 500; goAD.volume = volume; goAD.loop = loop;
  56. goAD.clip = clipDic[audioName];
  57. if (goAD.clip != null) goAD.Play();
  58. }
  59. /// <summary>
  60. /// 꺄렴供뎠품AudioManager稜틉헌왕
  61. /// </summary>
  62. public void AudioFinished()
  63. {
  64. StartCoroutine("Finished", ad);
  65. }
  66. /// <summary>
  67. /// 꺄렴供뎠품膠竟稜틉헌왕
  68. /// </summary>
  69. /// <param name="go"></param>
  70. public void AudioFinished(GameObject go)
  71. {
  72. AudioSource gAD = go.GetComponent<AudioSource>();
  73. if (gAD) StartCoroutine("Finished", gAD);
  74. }
  75. IEnumerator Finished(AudioSource audioSource)
  76. {
  77. yield return new WaitForSeconds(audioSource.clip.length);
  78. audioSource.clip = null;
  79. Destroy(audioSource);
  80. StopCoroutine("Finished");
  81. }
  82. /// <summary>
  83. /// 교쒼稜있숑흽돕轟
  84. /// </summary>
  85. public void Mute()
  86. {
  87. //anim.Play("bgmdown");
  88. }
  89. /// <summary>
  90. /// 교쒼稜있쉈퓻
  91. /// </summary>
  92. public void VolumeUp()
  93. {
  94. //anim.Play("bgmup");
  95. }
  96. /// <summary>
  97. /// 쓰稜 GameObject
  98. /// </summary>
  99. /// <param name="go"></param>
  100. public void Mute(GameObject go)
  101. {
  102. AudioSource gAD = go.GetComponent<AudioSource>();
  103. gAD.mute = true;
  104. }
  105. /// <summary>
  106. /// �零稜좆
  107. /// </summary>
  108. /// <param name="volume"></param>
  109. public void SetVolume(float volume)
  110. {
  111. ad.volume = volume;
  112. }
  113. }