WizardDuels/WizardDuelsProject/Assets/Scripts/AudioManager.cs
2025-03-19 09:23:22 +01:00

52 lines
1.1 KiB
C#

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<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}