Initial commit
This commit is contained in:
121
Assets/Resources/Scripts/Audio/AudioManager.cs
Normal file
121
Assets/Resources/Scripts/Audio/AudioManager.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioManager : MonoBehaviour
|
||||
{
|
||||
public static AudioManager Instance;
|
||||
public Sound[] musicSounds, sfxSounds;
|
||||
public AudioSource musicSource, sfxSource;
|
||||
|
||||
private Dictionary<string, Sound> musicDictionary = new Dictionary<string, Sound>();
|
||||
private Dictionary<string, Sound> sfxDictionary = new Dictionary<string, Sound>();
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
Setup();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Setup()
|
||||
{
|
||||
for (int i = 0; i < musicSounds.Length; i++)
|
||||
{
|
||||
musicDictionary[musicSounds[i].name] = musicSounds[i];
|
||||
}
|
||||
for (int i = 0; i < sfxSounds.Length; i++)
|
||||
{
|
||||
sfxDictionary[sfxSounds[i].name] = sfxSounds[i];
|
||||
}
|
||||
}
|
||||
public void StopMusic()
|
||||
{
|
||||
musicSource.Stop();
|
||||
}
|
||||
|
||||
public void PlayMusic(SoundName name)
|
||||
{
|
||||
Sound s = Array.Find(musicSounds, sound => sound.soundName == name);
|
||||
if (s != null)
|
||||
{
|
||||
musicSource.clip = s.clip;
|
||||
musicSource.Play();
|
||||
}
|
||||
}
|
||||
public void PlayMusic(string name)
|
||||
{
|
||||
Sound s = musicDictionary[name];
|
||||
if (s != null)
|
||||
{
|
||||
musicSource.clip = s.clip;
|
||||
|
||||
musicSource.Play();
|
||||
}
|
||||
}
|
||||
public void PlaySfx(string name)
|
||||
{
|
||||
Sound s = sfxDictionary[name];
|
||||
if (s != null)
|
||||
{
|
||||
sfxSource.PlayOneShot(s.clip);
|
||||
}
|
||||
}
|
||||
public void PlaySfx(AudioClip clip)
|
||||
{
|
||||
sfxSource.PlayOneShot(clip);
|
||||
|
||||
}
|
||||
public void PlaySfx(SoundName name)
|
||||
{
|
||||
|
||||
Sound s = Array.Find(sfxSounds, sound => sound.soundName == name);
|
||||
if (s != null)
|
||||
{
|
||||
sfxSource.PlayOneShot(s.clip);
|
||||
}
|
||||
}
|
||||
public void PlaySfx(Sound sound)
|
||||
{
|
||||
if (sound != null)
|
||||
{
|
||||
sfxSource.PlayOneShot(sound.clip);
|
||||
}
|
||||
}
|
||||
public void PlaySfx(Sound[] randomSound)
|
||||
{
|
||||
|
||||
if (randomSound != null)
|
||||
{
|
||||
sfxSource.PlayOneShot(randomSound[UnityEngine.Random.Range(0, randomSound.Length)].clip);
|
||||
}
|
||||
}
|
||||
public void PlaySfx(SoundName[] randomName)
|
||||
{
|
||||
SoundName name = randomName[UnityEngine.Random.Range(0, randomName.Length)];
|
||||
Sound s = Array.Find(sfxSounds, sound => sound.soundName == name);
|
||||
if (s != null)
|
||||
{
|
||||
sfxSource.PlayOneShot(s.clip);
|
||||
}
|
||||
}
|
||||
public void SetSfxVolume(float volume)
|
||||
{
|
||||
sfxSource.volume = volume;
|
||||
}
|
||||
public void SetMusicVolume(float volume)
|
||||
{
|
||||
musicSource.volume = volume;
|
||||
}
|
||||
}
|
||||
|
11
Assets/Resources/Scripts/Audio/AudioManager.cs.meta
Normal file
11
Assets/Resources/Scripts/Audio/AudioManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f5ac05904ff5ab4882b8026e7f21813
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
Assets/Resources/Scripts/Audio/Sound.cs
Normal file
11
Assets/Resources/Scripts/Audio/Sound.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class Sound
|
||||
{
|
||||
public string name;
|
||||
public SoundName soundName;
|
||||
public AudioClip clip;
|
||||
|
||||
}
|
||||
|
11
Assets/Resources/Scripts/Audio/Sound.cs.meta
Normal file
11
Assets/Resources/Scripts/Audio/Sound.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 273e91268ea03254f9cde0aa8362ab44
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Assets/Resources/Scripts/Audio/SoundList.cs
Normal file
9
Assets/Resources/Scripts/Audio/SoundList.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
|
||||
public class SoundList
|
||||
{
|
||||
[SerializeField] private Sound[] soundList;
|
||||
|
||||
}
|
11
Assets/Resources/Scripts/Audio/SoundList.cs.meta
Normal file
11
Assets/Resources/Scripts/Audio/SoundList.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0ad2eabaf4aefc409fddc2622504ce3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
6
Assets/Resources/Scripts/Audio/SoundName.cs
Normal file
6
Assets/Resources/Scripts/Audio/SoundName.cs
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
public enum SoundName
|
||||
{
|
||||
GameMusic, MainMenuMusic
|
||||
}
|
||||
|
11
Assets/Resources/Scripts/Audio/SoundName.cs.meta
Normal file
11
Assets/Resources/Scripts/Audio/SoundName.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 687137de75e342b4792ac6fdb52a4321
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user