GlobalGameJam-2021/Assets/Scripts/AudioManager.cs

37 lines
966 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 14:51:03 +00:00
public void Play(string name, bool checkIfPlay = false)
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 13:22:06 +00:00
Debug.Log("Sound is playng");
2021-01-31 14:51:03 +00:00
if (!s.source.isPlaying || !checkIfPlay) {
s.source.Play();
}
2021-01-31 10:28:34 +00:00
}
}