Last active
August 29, 2015 14:22
-
-
Save TakaakiIchijo/e303c1c8f5848a658fa1 to your computer and use it in GitHub Desktop.
Unity オレオレサウンドマネージャー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System; | |
public class SoundManager : SingletonMonoBehaviour<SoundManager>,IPausable { | |
//From resources folder// | |
public string seAudioClipPath = "AudioClips/SE"; | |
public string bgmAudioClipPath = "AudioClips/BGM"; | |
public int seMaxNum = 5;//SEの同時再生上限数// | |
private static List<AudioSource> audioSourceSEs = new List<AudioSource>(); | |
private static List<AudioSource> audioSourceBGMs = new List<AudioSource>(); | |
//読み込むサウンドデータのリスト// | |
private static List<AudioClip> seAudioClips = new List<AudioClip>(); | |
private static List<AudioClip> bgmAudioClips = new List<AudioClip>(); | |
private static string currentBgmClipName;//再生中、または再生へ向けてフェードイン中のbgm; | |
public float masterVolume = 1.0f;//インスペクタから弄る用// | |
public float seVolume = 0.5f;//インスペクタから弄る用// | |
public float bgmVolume = 0.5f;//インスペクタから弄る用// | |
public static float _masterVolume = 1.0f;//全体ボリューム// | |
public static float _fixedSeVolume = 1.0f;//masterと掛け算したseVolume// | |
public static float _fixedBgmVolume = 1.0f;//masterと掛け算したbgmVolume// | |
public bool isDebugMuteMode = false; | |
[SerializeField] | |
private bool isPaused = false; | |
public bool IsPaused | |
{ | |
get | |
{ | |
return isPaused; | |
} | |
} | |
public bool enableDebugMonitor = false; | |
private static bool _enableDebugMonitor; | |
private static GameObject thisGameObject; | |
private static float defaultFadeTime = 2f; | |
void Awake() | |
{ | |
base.CheckInstance(); | |
_masterVolume = masterVolume; | |
_fixedSeVolume = masterVolume * seVolume; | |
_fixedBgmVolume = masterVolume * bgmVolume; | |
_enableDebugMonitor = enableDebugMonitor; | |
DontDestroyOnLoad(this.gameObject); | |
thisGameObject = this.gameObject; | |
//リソース読み込み(同期読み)// | |
seAudioClips = LoadAudioClipsFromResourcesFolder(seAudioClipPath); | |
bgmAudioClips = LoadAudioClipsFromResourcesFolder(bgmAudioClipPath); | |
//オーディオソースの生成// | |
audioSourceBGMs = InstantiateAudioSourceList(2); | |
audioSourceSEs = InstantiateAudioSourceList(seMaxNum); | |
audioSourceBGMs.ForEach(asb => asb.loop = true); | |
} | |
public static void PlaySE(string clipName) | |
{ | |
PlaySE(clipName, _fixedSeVolume); | |
} | |
public static void PlaySE(string clipName, float volume) | |
{ | |
//空いているaudioSourceはあるか?// | |
AudioSource _audioSource = GetStoppedAudioSoureFromList(audioSourceSEs); | |
//clipNameのaudioClipはロードされているか?// | |
_audioSource.clip = GetAudioClipFromRoadedList(clipName, seAudioClips); | |
_audioSource.volume = volume; | |
_audioSource.PlayOneShot( GetAudioClipFromRoadedList(clipName, seAudioClips)); | |
if (_enableDebugMonitor) Debug.Log(clipName + "が再生されました。"); | |
} | |
public void OnPause() | |
{ | |
isPaused = true; | |
audioSourceSEs.ForEach(ase => ase.Pause()); | |
audioSourceBGMs.ForEach(asb => asb.Pause()); | |
} | |
public void OnResume() | |
{ | |
isPaused = false; | |
audioSourceSEs.ForEach(ase => ase.UnPause()); | |
audioSourceBGMs.ForEach(asb => asb.UnPause()); | |
} | |
public static void PlayBGM(string clipName) | |
{ | |
PlayBGMWithFade(clipName, 0.1f); | |
} | |
public static void PlayBGMWithFade(string clipName) | |
{ | |
PlayBGMWithFade(clipName, defaultFadeTime); | |
} | |
public static void PlayBGMWithFade(string clipName, float fadeTime) | |
{ | |
//all audio source is playing (already crossfading?) | |
if (audioSourceBGMs.All(asb => asb.isPlaying == true)) | |
return; | |
AudioSource _toStopAudioSource = audioSourceBGMs.FirstOrDefault(asb => asb.isPlaying == true); | |
AudioSource _toPlayAudioSource = audioSourceBGMs.First(asb => asb.isPlaying == false); | |
if (_toStopAudioSource != null) | |
{ | |
FadeOutAudioSource(_toStopAudioSource, fadeTime); | |
} | |
_toPlayAudioSource.clip = GetAudioClipFromRoadedList(clipName, bgmAudioClips); | |
FadeInAudioSource(_toPlayAudioSource, fadeTime); | |
} | |
public static void StopBGM() | |
{ | |
audioSourceBGMs.ForEach(asb => FadeOutAudioSource(asb, 0.1f)); | |
} | |
static void FadeInAudioSource(AudioSource targetAudioSource, float fadeTime) | |
{ | |
targetAudioSource.Play(); | |
targetAudioSource.volume = 0f; | |
iTween.AudioTo(thisGameObject, iTween.Hash( | |
"audiosource", targetAudioSource, | |
"time", fadeTime,//3秒かけて// | |
"volume", 1f//ボリュームを1に// | |
)); | |
} | |
static void FadeOutAudioSource(AudioSource targetAudioSource, float fadeTime) | |
{ | |
iTween.AudioTo(thisGameObject, iTween.Hash( | |
"audiosource", targetAudioSource, | |
"time", fadeTime,//3秒かけて// | |
"volume", 0f,//ボリュームをゼロに// | |
"oncomplete", "OnFadeOutComplete", | |
"oncompletetarget", thisGameObject, | |
"oncompleteparams", targetAudioSource | |
)); | |
} | |
void OnFadeOutComplete(AudioSource targetAudioSource) | |
{ | |
targetAudioSource.Stop(); | |
} | |
#region Util | |
List<AudioSource> InstantiateAudioSourceList(int num) | |
{ | |
List<AudioSource> _audioSourceList = new List<AudioSource>(); | |
for (int i = 0; i < num - 1; i++) | |
{ | |
AudioSource _audioSource = this.gameObject.AddComponent<AudioSource>() as AudioSource; | |
_audioSource.playOnAwake = false; | |
_audioSourceList.Add(_audioSource); | |
} | |
return _audioSourceList; | |
} | |
static AudioSource GetStoppedAudioSoureFromList(List<AudioSource> audioSourceList) | |
{ | |
AudioSource _audioSource = audioSourceList.FirstOrDefault(i => i.isPlaying == false); | |
if (_audioSource == null) | |
{ | |
if (_enableDebugMonitor) | |
Debug.LogWarning("audioSourceSEの再生上限です。"); | |
} | |
return _audioSource; | |
} | |
private static AudioClip GetAudioClipFromRoadedList(string clipName, List<AudioClip> clipList) | |
{ | |
AudioClip _clip = clipList.Find(i => i.name == clipName); | |
if (_clip == null) | |
{ | |
if (_enableDebugMonitor) | |
Debug.LogWarning("オーディオクリップ「" + clipName + "」は読み込まれていません。"); | |
} | |
return _clip; | |
} | |
private List<AudioClip> LoadAudioClipsFromResourcesFolder(string path) | |
{ | |
return Resources.LoadAll(path, typeof(AudioClip)).Cast<AudioClip>().ToList(); | |
} | |
//Resoucesフォルダ内のパスを列挙する方法が見つかったら使用する// | |
IEnumerator LoadAsyncAudioClip() | |
{ | |
ResourceRequest request = Resources.LoadAsync("AudioClips/SE/Footstep_L"); | |
yield return request; | |
seAudioClips.Add(request.asset as AudioClip); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment