기본 사운드 기능 및 사운드 기능을 위한 사운드 매니저 추가
This commit is contained in:
@@ -38,4 +38,10 @@ public class InputManager
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
KeytAction = null;
|
||||
MouseAction = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,13 @@ public class Managers : MonoBehaviour
|
||||
InputManager _input = new InputManager();
|
||||
ResourceManager _resource = new ResourceManager();
|
||||
SceneManagerEx _scene = new SceneManagerEx();
|
||||
SoundManager _sound = new SoundManager();
|
||||
UIManager _ui = new UIManager();
|
||||
|
||||
public static InputManager Input { get { return Instance._input; } }
|
||||
public static ResourceManager Resource { get { return Instance._resource; } }
|
||||
public static SceneManagerEx Scene { get { return Instance._scene; } }
|
||||
public static SoundManager Sound { get { return Instance._sound; } }
|
||||
public static UIManager UI { get { return Instance._ui; } }
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
@@ -39,7 +42,17 @@ public class Managers : MonoBehaviour
|
||||
}
|
||||
|
||||
DontDestroyOnLoad(go);
|
||||
s_instance = go.GetComponent<Managers>();
|
||||
s_instance = go.GetComponent<Managers>();
|
||||
|
||||
s_instance._sound.Init();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
Sound.Clear();
|
||||
Input.Clear();
|
||||
Scene.Clear();
|
||||
UI.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class SceneManagerEx
|
||||
|
||||
public void LoadScene(Define.Scene type)
|
||||
{
|
||||
CurrentScene.Clear();
|
||||
Managers.Clear();
|
||||
SceneManager.LoadScene(GetSceneName(type));
|
||||
}
|
||||
|
||||
@@ -17,4 +17,9 @@ public class SceneManagerEx
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
CurrentScene.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Managers/SoundManager.cs.meta
Normal file
2
Assets/Scripts/Managers/SoundManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea047cbd9d3f904469f3c8cee25caea5
|
||||
@@ -126,5 +126,10 @@ public class UIManager
|
||||
ClosePopupUI();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
CloseAllPopupUI();
|
||||
_sceneUI = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user