Update AudioManager.cs

This commit is contained in:
DerTyp187
2022-01-02 14:05:57 +01:00
parent be50ec5eed
commit 9027425c70

View File

@@ -1,9 +1,11 @@
using UnityEditor.Audio; using UnityEngine.Audio;
using System; using System;
using UnityEngine; using UnityEngine;
public class AudioManager : MonoBehaviour public class AudioManager : MonoBehaviour
{ {
// This list contains all sounds which should be accessable by the audiomanager.
// If you want to add a sound -> add a new item in the list VIA the inspector!
public Sound[] sounds; public Sound[] sounds;
public static AudioManager instance; public static AudioManager instance;
@@ -17,14 +19,14 @@ public class AudioManager : MonoBehaviour
Destroy(gameObject); Destroy(gameObject);
return; return;
} }
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
foreach(Sound s in sounds) foreach(Sound s in sounds)
{ {
s.source = gameObject.AddComponent<AudioSource>(); s.source = gameObject.AddComponent<AudioSource>(); // The Audio source where the "Player can hear through"
s.source.clip = s.clip;
s.source.clip = s.clip;
s.source.volume = s.volume; s.source.volume = s.volume;
s.source.pitch = s.pitch; s.source.pitch = s.pitch;
s.source.loop = s.loop; s.source.loop = s.loop;
@@ -37,9 +39,10 @@ public class AudioManager : MonoBehaviour
} }
// USE this to play a sound in other scripts: FindObjectOfType<AudioManager>().Play(name);
public void Play(string name) public void Play(string name)
{ {
Sound s = Array.Find(sounds, sound => sound.name == name); Sound s = Array.Find(sounds, sound => sound.name == name); // searching for sound in Array where sound.name == name
if(s == null) if(s == null)
{ {
Debug.LogWarning("Sound: " + s.name + " not found!"); Debug.LogWarning("Sound: " + s.name + " not found!");