SoundMgr.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DG.Tweening;
  5. using UnityEngine;
  6. [Serializable]
  7. public class SoundMgr : MonoBehaviour
  8. {
  9. public static SoundMgr Instance
  10. {
  11. get
  12. {
  13. if (SoundMgr._instance == null)
  14. {
  15. SoundMgr._instance = UnityEngine.Object.FindObjectOfType<SoundMgr>();
  16. }
  17. return SoundMgr._instance;
  18. }
  19. }
  20. private void Awake()
  21. {
  22. for (int i = 0; i < this.sfxs.Length; i++)
  23. {
  24. AudioSource audioSource = new GameObject("audiosource_sfx")
  25. {
  26. transform =
  27. {
  28. parent = base.transform
  29. }
  30. }.AddComponent<AudioSource>();
  31. audioSource.clip = this.sfxs[i].clip;
  32. this.sfxs[i].audioSource = audioSource;
  33. this.sfxTable.Add(this.sfxs[i].name, this.sfxs[i]);
  34. }
  35. }
  36. private void _playSfx(string name, float volume, bool loop, float fadeinTime = 0f)
  37. {
  38. if (!this.sfxTable.ContainsKey(name))
  39. {
  40. return;
  41. }
  42. AudioSource audioSource = this.sfxTable[name].audioSource;
  43. if (audioSource != null)
  44. {
  45. audioSource.loop = loop;
  46. if (loop)
  47. {
  48. if (fadeinTime != 0f)
  49. {
  50. audioSource.volume = 0f;
  51. audioSource.Play();
  52. audioSource.DOFade(volume, fadeinTime);
  53. }
  54. else
  55. {
  56. audioSource.volume = volume;
  57. audioSource.Play();
  58. }
  59. }
  60. else
  61. {
  62. audioSource.volume = volume;
  63. audioSource.PlayOneShot(this.sfxTable[name].clip, volume);
  64. }
  65. }
  66. }
  67. private IEnumerator _playSfxAfterTime(string name, float volume, bool loop, float waitTime, float fadeinTime = 0f)
  68. {
  69. yield return new WaitForSeconds(waitTime);
  70. this._playSfx(name, volume, loop, fadeinTime);
  71. yield break;
  72. }
  73. public void PlaySfx(string name, float volume, bool loop, float waitTime = 0f, float fadeinTime = 0f)
  74. {
  75. volume *= R.Audio.EffectsVolume / 100f;
  76. if (waitTime > 0f)
  77. {
  78. base.StartCoroutine(this._playSfxAfterTime(name, volume, loop, waitTime, fadeinTime));
  79. }
  80. else
  81. {
  82. this._playSfx(name, volume, loop, fadeinTime);
  83. }
  84. }
  85. public void StopSfx(string name, float fadeoutTime = 0f)
  86. {
  87. if (!this.sfxTable.ContainsKey(name))
  88. {
  89. return;
  90. }
  91. AudioSource audioSource = this.sfxTable[name].audioSource;
  92. if (audioSource != null)
  93. {
  94. audioSource.DOFade(0f, fadeoutTime).OnComplete(delegate
  95. {
  96. audioSource.Stop();
  97. });
  98. }
  99. }
  100. public AudioSet[] sfxs;
  101. private Dictionary<string, AudioSet> sfxTable = new Dictionary<string, AudioSet>();
  102. private static SoundMgr _instance;
  103. }