GlobalGameJam-2021/Assets/Scripts/AudioManager.cs

34 lines
837 B
C#
Raw Normal View History

2021-01-31 10:28:34 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
2021-01-31 10:43:20 +00:00
using System;
2021-01-31 10:28:34 +00:00
2021-01-31 10:58:07 +00:00
public class AudioManager : MonoBehaviour
2021-01-31 10:28:34 +00:00
{
2021-01-31 10:43:20 +00:00
public AudioSound[] clips;
2021-01-31 10:28:34 +00:00
// Start is called before the first frame update
2021-01-31 10:58:07 +00:00
public void Awake()
2021-01-31 10:28:34 +00:00
{
2021-01-31 11:07:05 +00:00
DontDestroyOnLoad(gameObject);
2021-01-31 10:43:20 +00:00
foreach (AudioSound c in clips)
2021-01-31 10:28:34 +00:00
{
2021-01-31 10:52:14 +00:00
c.source = gameObject.AddComponent<AudioSource>();
2021-01-31 10:43:20 +00:00
c.source.clip = c.clip;
c.source.volume = c.volume;
c.source.pitch = c.pitch;
2021-01-31 11:02:31 +00:00
c.source.loop = c.loop;
2021-01-31 10:28:34 +00:00
}
}
// Update is called once per frame
2021-01-31 10:58:07 +00:00
public void Play(string name)
2021-01-31 10:28:34 +00:00
{
2021-01-31 10:43:20 +00:00
AudioSound s = Array.Find(clips, sound => sound.name == name);
2021-01-31 11:02:31 +00:00
if (s == null) {
return;
}
2021-01-31 10:43:20 +00:00
s.source.Play();
2021-01-31 10:28:34 +00:00
}
}