기본 사운드 기능 및 사운드 기능을 위한 사운드 매니저 추가
This commit is contained in:
109
Assets/Scripts/Managers/SoundManager.cs
Normal file
109
Assets/Scripts/Managers/SoundManager.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SoundManager
|
||||
{
|
||||
AudioSource[] _audioSources = new AudioSource[(int)Define.Sound.MaxCount];
|
||||
Dictionary<string, AudioClip> _audioClips = new Dictionary<string, AudioClip>();
|
||||
|
||||
// Mp3 Player -> AudioSource
|
||||
// Mp3 음원? -> AudioClip
|
||||
// 관객(귀) -> AudioListener
|
||||
|
||||
public void Init()
|
||||
{
|
||||
GameObject root = GameObject.Find("@Sound");
|
||||
|
||||
if (root == null)
|
||||
{
|
||||
root = new GameObject("@Sound");
|
||||
Object.DontDestroyOnLoad(root);
|
||||
|
||||
string[] soundNames = System.Enum.GetNames(typeof(Define.Sound));
|
||||
|
||||
for (int i = 0; i < soundNames.Length -1; i++)
|
||||
{
|
||||
GameObject go = new GameObject { name = soundNames[i] };
|
||||
_audioSources[i] = go.AddComponent<AudioSource>();
|
||||
go.transform.parent = root.transform;
|
||||
}
|
||||
|
||||
_audioSources[(int)Define.Sound.Bgm].loop = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (AudioSource audioSource in _audioSources)
|
||||
{
|
||||
audioSource.clip = null;
|
||||
audioSource.Stop();
|
||||
}
|
||||
_audioClips.Clear();
|
||||
}
|
||||
|
||||
public void Play(string path, Define.Sound type = Define.Sound.Effect, float pitch = 1.0f)
|
||||
{
|
||||
AudioClip audioClip = GetOrAddAudioClip(path, type);
|
||||
|
||||
Play(audioClip, type, pitch);
|
||||
}
|
||||
|
||||
public void Play(AudioClip audioClip, Define.Sound type = Define.Sound.Effect, float pitch = 1.0f)
|
||||
{
|
||||
if (audioClip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == Define.Sound.Bgm)
|
||||
{
|
||||
AudioSource audioSource = _audioSources[(int)Define.Sound.Bgm];
|
||||
|
||||
if (audioSource.isPlaying)
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
|
||||
audioSource.pitch = pitch;
|
||||
audioSource.clip = audioClip;
|
||||
audioSource.Play();
|
||||
}
|
||||
else
|
||||
{
|
||||
AudioSource audioSource = _audioSources[(int)Define.Sound.Effect];
|
||||
audioSource.pitch = pitch;
|
||||
audioSource.PlayOneShot(audioClip);
|
||||
}
|
||||
}
|
||||
|
||||
AudioClip GetOrAddAudioClip(string path, Define.Sound type = Define.Sound.Effect)
|
||||
{
|
||||
if (path.Contains("Sounds/") == false)
|
||||
{
|
||||
path = $"Sounds/{path}";
|
||||
}
|
||||
|
||||
AudioClip audioClip = null;
|
||||
|
||||
if (type == Define.Sound.Bgm)
|
||||
{
|
||||
audioClip = Managers.Resource.Load<AudioClip>(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_audioClips.TryGetValue(path, out audioClip) == false)
|
||||
{
|
||||
audioClip = Managers.Resource.Load<AudioClip>(path);
|
||||
_audioClips.Add(path, audioClip);
|
||||
}
|
||||
}
|
||||
|
||||
if (audioClip == null)
|
||||
{
|
||||
Debug.Log($"AudioClip Missing ! {path}");
|
||||
}
|
||||
|
||||
return audioClip;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user