mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-30 13:07:10 +01:00
created audio manager and Sound class
This commit is contained in:
51
Assets/Scripts/Audio/AudioManager.cs
Normal file
51
Assets/Scripts/Audio/AudioManager.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using UnityEditor.Audio;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioManager : MonoBehaviour
|
||||
{
|
||||
public Sound[] sounds;
|
||||
|
||||
public static AudioManager instance;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if(instance == null)
|
||||
instance = this;
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
foreach(Sound s in sounds)
|
||||
{
|
||||
s.source = gameObject.AddComponent<AudioSource>();
|
||||
s.source.clip = s.clip;
|
||||
|
||||
s.source.volume = s.volume;
|
||||
s.source.pitch = s.pitch;
|
||||
s.source.loop = s.loop;
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Enter Music here
|
||||
}
|
||||
|
||||
|
||||
public void Play(string name)
|
||||
{
|
||||
Sound s = Array.Find(sounds, sound => sound.name == name);
|
||||
if(s == null)
|
||||
{
|
||||
Debug.LogWarning("Sound: " + s.name + " not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
s.source.Play();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user