| | | 1 | | using UnityEngine.Audio; |
| | | 2 | | using System; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | public class AudioManager : MonoBehaviour |
| | | 6 | | { |
| | | 7 | | |
| | | 8 | | public static AudioManager instance; |
| | | 9 | | |
| | | 10 | | public AudioMixerGroup mixerGroup; |
| | | 11 | | |
| | | 12 | | public Sound[] sounds; |
| | | 13 | | |
| | | 14 | | void Awake() |
| | 1 | 15 | | { |
| | 1 | 16 | | if (instance != null) |
| | 0 | 17 | | { |
| | 0 | 18 | | Destroy(gameObject); |
| | 0 | 19 | | } |
| | | 20 | | else |
| | 1 | 21 | | { |
| | 1 | 22 | | instance = this; |
| | 1 | 23 | | DontDestroyOnLoad(gameObject); |
| | 1 | 24 | | } |
| | | 25 | | |
| | 9 | 26 | | foreach (Sound s in sounds) |
| | 3 | 27 | | { |
| | 3 | 28 | | s.source = gameObject.AddComponent<AudioSource>(); |
| | 3 | 29 | | s.source.clip = s.clip; |
| | 3 | 30 | | s.source.loop = s.loop; |
| | | 31 | | |
| | 3 | 32 | | s.source.outputAudioMixerGroup = mixerGroup; |
| | 3 | 33 | | } |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | public void Play(string sound) |
| | 53 | 37 | | { |
| | 111 | 38 | | Sound s = Array.Find(sounds, item => item.name == sound); |
| | 53 | 39 | | if (s == null) |
| | 0 | 40 | | { |
| | 0 | 41 | | Debug.LogWarning("Sound: " + name + " not found!"); |
| | | 42 | | //return; |
| | 0 | 43 | | } |
| | | 44 | | |
| | 53 | 45 | | s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f)); |
| | 53 | 46 | | s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f)); |
| | | 47 | | |
| | 53 | 48 | | s.source.Play(); |
| | 53 | 49 | | } |
| | | 50 | | |
| | | 51 | | } |