AudioManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices.WindowsRuntime;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. public enum SoundCategory
  8. {
  9. FruitSmash,
  10. BombExplode,
  11. ArrowFire,
  12. CannonFire,
  13. None,
  14. GunFire,
  15. }
  16. // This class in instantiated on GameManager, storing all the audio related assets
  17. public class AudioManager : MonoBehaviour
  18. {
  19. [SerializeField] private AudioClip[] FruitSmashSounds;
  20. [SerializeField] private AudioClip[] BombExplodeSounds;
  21. [SerializeField] private AudioClip[] ArrowFiringSounds;
  22. [SerializeField] private AudioClip[] CannonFiringSounds;
  23. [SerializeField] private AudioClip[] GunSounds;
  24. [SerializeField] private AudioClip LooseLifeSound;
  25. [SerializeField] private AudioClip BackgroundMusic;
  26. [SerializeField] private AudioClip GameOverSound;
  27. public AudioClip GetRandomSound(SoundCategory cat)
  28. {
  29. AudioClip[] clips;
  30. switch (cat)
  31. {
  32. case SoundCategory.FruitSmash:
  33. clips = FruitSmashSounds;
  34. break;
  35. case SoundCategory.BombExplode:
  36. clips = BombExplodeSounds;
  37. break;
  38. case SoundCategory.ArrowFire:
  39. clips = ArrowFiringSounds;
  40. break;
  41. case SoundCategory.CannonFire:
  42. clips = CannonFiringSounds;
  43. break;
  44. case SoundCategory.GunFire:
  45. clips = GunSounds ;
  46. break;
  47. default:
  48. clips = null;
  49. break;
  50. }
  51. Debug.Assert(clips.Length > 0);
  52. return clips[Random.Range(0, clips.Length)];
  53. }
  54. public GameObject InstantiateTemprorarySound(SoundCategory cat, Transform parent, float delay_dstroy_s = 0f)
  55. {
  56. GameObject empty = new GameObject();
  57. GameObject instance = Instantiate(empty, parent);
  58. AudioSource audio = instance.AddComponent<AudioSource>();
  59. if (cat != SoundCategory.None)
  60. {
  61. AudioClip clip = GetRandomSound(cat);
  62. audio.clip = clip;
  63. if (UserSettings.ins.openEffect) audio.Play();
  64. }
  65. if (delay_dstroy_s > 0f)
  66. {
  67. GamingManager.DelayDestroy(this, instance, delay_dstroy_s);
  68. }
  69. Destroy(empty);
  70. return instance;
  71. }
  72. public GameObject InstantiateTemprorarySound(SoundCategory cat, Vector3 position, float delay_dstroy_s = 0f)
  73. {
  74. GameObject empty = new GameObject();
  75. GameObject instance = Instantiate(empty, position, Quaternion.identity);
  76. AudioSource audio = instance.AddComponent<AudioSource>();
  77. if (cat != SoundCategory.None)
  78. {
  79. AudioClip clip = GetRandomSound(cat);
  80. audio.clip = clip;
  81. if (UserSettings.ins.openEffect) audio.Play();
  82. }
  83. if (delay_dstroy_s > 0f)
  84. {
  85. GamingManager.DelayDestroy(this, instance, delay_dstroy_s);
  86. }
  87. Destroy(empty);
  88. return instance;
  89. }
  90. public AudioClip GetBackgroundMusic()
  91. {
  92. return BackgroundMusic;
  93. }
  94. public AudioClip GetLooseLifeSound()
  95. {
  96. return LooseLifeSound;
  97. }
  98. public AudioClip GetGameOverSound()
  99. {
  100. return GameOverSound;
  101. }
  102. }