Guest User

Untitled

a guest
Jul 6th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SoundFXHandler : MonoBehaviour
  6. {
  7. public int maxSources = 25;
  8. public SoundFX[] sounds;
  9. private List<AudioSource> audioSources = new List<AudioSource>();
  10.  
  11. private static SoundFXHandler _instance;
  12. public static SoundFXHandler Instance { get { return _instance; } }
  13.  
  14. private void Awake()
  15. {
  16. if (_instance != null && _instance != this)
  17. Destroy(gameObject);
  18. else
  19. _instance = this;
  20. }
  21.  
  22. public void PlaySound (string fxName, Vector3 location)
  23. {
  24. AudioSource audioSource = GetAudioSource();
  25.  
  26. if (audioSource == null)
  27. return;
  28.  
  29. foreach (SoundFX soundFX in sounds)
  30. {
  31. if (fxName == soundFX.name)
  32. {
  33. audioSource.transform.position = location;
  34. soundFX.Attach(audioSource);
  35. return;
  36. }
  37. }
  38.  
  39. Debug.LogWarning("Could not find SoundFX, \"" + fxName + " \" in sounds array");
  40. }
  41.  
  42. public AudioSource PlaySoundRaw(AudioClip clip, Vector3 location)
  43. {
  44.  
  45. AudioSource audioSource = GetAudioSource();
  46.  
  47. if (audioSource == null)
  48. return null;
  49.  
  50.  
  51. audioSource.transform.position = location;
  52. audioSource.spatialBlend = 1f;
  53. audioSource.clip = clip;
  54. audioSource.Play();
  55. return audioSource;
  56. }
  57.  
  58. public void PlaySound2DRaw(AudioClip clip)
  59. {
  60.  
  61. AudioSource audioSource = GetAudioSource();
  62.  
  63. if (audioSource == null)
  64. return;
  65.  
  66. audioSource.spatialBlend = 0f;
  67. audioSource.clip = clip;
  68. audioSource.Play();
  69.  
  70. }
  71.  
  72. public AudioSource GetSound2DRaw(AudioClip clip)
  73. {
  74.  
  75. AudioSource audioSource = GetAudioSource();
  76.  
  77. if (audioSource == null)
  78. return null;
  79.  
  80. audioSource.name = clip.name;
  81. audioSource.spatialBlend = 0f;
  82. audioSource.clip = clip;
  83.  
  84. return audioSource;
  85.  
  86. }
  87.  
  88. private AudioSource GetAudioSource ()
  89. {
  90. // foreach (AudioSource source in audioSources)
  91. // {
  92. // if (!source.isPlaying)
  93. // return source;
  94. // }
  95.  
  96. if (audioSources.Count < maxSources)
  97. {
  98. GameObject newAudioSource = new GameObject();
  99. newAudioSource.transform.SetParent(transform);
  100. audioSources.Add(newAudioSource.AddComponent<AudioSource>());
  101. return audioSources[audioSources.Count - 1];
  102. }
  103.  
  104. Debug.LogWarning("No available audio sources found and maximum number of audio sources reached.");
  105. return null;
  106. }
  107. }
  108.  
  109. [System.Serializable]
  110. public class SoundFX
  111. {
  112. #region Initialise Variables
  113. [Header("FX Details")]
  114. public string name = "New Sound";
  115. public AudioClip clip = null;
  116.  
  117. [Header("FX Settings")]
  118. public bool loop = false;
  119. [Range(0f, 1f)]
  120. public float volume = 1f;
  121. #endregion
  122.  
  123. public SoundFX (AudioClip _clip)
  124. {
  125. clip = _clip;
  126. }
  127.  
  128. public SoundFX (string _name, AudioClip _clip)
  129. {
  130. name = _name;
  131. clip = _clip;
  132. }
  133.  
  134. public void Attach (AudioSource audioSource)
  135. {
  136. audioSource.clip = clip;
  137. audioSource.loop = loop;
  138. audioSource.volume = volume;
  139. audioSource.spatialBlend = 1f;
  140.  
  141. audioSource.Play();
  142. }
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment