using System; using DefaultNamespace; using UnityEngine.Audio; using UnityEngine; public class AudioManager : MonoBehaviour { public Sound[] sounds; public SoundGroup[] soundGroups; private void Awake() { foreach (SoundGroup group in soundGroups) { foreach (Sound s in group.sounds) { InnitializeSound(s); } } foreach (Sound s in sounds) { InnitializeSound(s); } } public void Play(string name) { SoundGroup sg = Array.Find(soundGroups, group => group.name == name); if (sg != null) { sg.Play(); return; } Sound s = Array.Find(sounds, sound => sound.name == name); if (s == null) { Debug.LogWarning($"Sound: {name} not found!"); return; } s.source.Play(); } private void InnitializeSound(Sound s) { s.source = gameObject.AddComponent(); s.source.clip = s.clip; s.source.volume = s.volume; s.source.pitch = s.pitch; } }