Last active
May 23, 2019 19:41
-
-
Save nekonenene/0f2b9702c4953c8a43c42788affb59c5 to your computer and use it in GitHub Desktop.
オーディオのフェードイン・フェードアウトを DOTween を使わずに実装する(ブログ公開用)
This file contains 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 System; | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.Audio; | |
// Singleton として持ち続ける | |
public class SoundManager : MonoBehaviour { | |
static SoundManager instance; | |
public AudioMixerGroup BGMMixerGroup; | |
public AudioMixerGroup SEMixerGroup; | |
GameObject soundObject; | |
AudioMixer mixer; | |
AudioSource bgmSource; | |
FadeComponent fadeComponent; | |
static string bgmVolumeParam = "BGMVolume"; | |
static string seVolumeParam = "SEVolume"; | |
static float defaultVolume = 0.8f; // 0 dB | |
void Awake() { | |
if (instance == null) { | |
instance = this; | |
mixer = BGMMixerGroup.audioMixer; | |
createSoundObject(); | |
DontDestroyOnLoad(this.gameObject); | |
} else { | |
Destroy(this.gameObject); | |
} | |
} | |
void createSoundObject() { | |
soundObject = new GameObject("SoundObject"); | |
GameObject.DontDestroyOnLoad(soundObject); | |
bgmSource = soundObject.AddComponent<AudioSource>(); | |
} | |
public static void PlayBGM(string path) { | |
instance.playBGM(path); | |
} | |
void playBGM(string path) { | |
if (fadeComponent != null) fadeComponent.Stop(); | |
SetBGMMixerVolume(defaultVolume); | |
var clip = Resources.Load<AudioClip>(path); | |
bgmSource.clip = clip; | |
bgmSource.outputAudioMixerGroup = BGMMixerGroup; | |
bgmSource.loop = true; | |
bgmSource.Play(); | |
Resources.UnloadUnusedAssets(); | |
} | |
public static void SetBGMMixerVolume(float volume = 0.8f) { | |
instance.mixer.SetFloat(bgmVolumeParam, volumeToDecibel(volume)); | |
} | |
public static void SetSEMixerVolume(float volume = 0.8f) { | |
instance.mixer.SetFloat(seVolumeParam, volumeToDecibel(volume)); | |
} | |
static float volumeToDecibel(float volume) { | |
return volume * 100 - 80; // -80dB ~ +20dB | |
} | |
public static void FadeInBGM(float fadeDuration = 2.0f) { | |
instance.fadeStart(FadeComponent.ModeSet.FadeIn, defaultVolume, fadeDuration); | |
} | |
public static void FadeOutBGM(float fadeDuration = 2.0f) { | |
instance.fadeStart(FadeComponent.ModeSet.FadeOut, defaultVolume, fadeDuration); | |
} | |
void fadeStart(int fadeMode, float maxVolume = 0.8f, float fadeDuration = 2.0f) { | |
fadeComponent = soundObject.AddComponent<FadeComponent>(); | |
fadeComponent.Mode = fadeMode; | |
fadeComponent.MaxVolume = maxVolume; | |
fadeComponent.FadeDuration = fadeDuration; | |
fadeComponent.Start(); | |
} | |
public class FadeComponent : MonoBehaviour { | |
public float MaxVolume; | |
public float FadeDuration; | |
public int Mode; | |
public struct ModeSet { | |
public static int FadeIn = 1, FadeOut = 2; | |
} | |
float startVolume; | |
float endVolume; | |
float elapsedTime; // 経過時間 | |
public void Start() { | |
StartCoroutine(fadeCoroutine()); | |
} | |
public void Stop() { | |
StopCoroutine(fadeCoroutine()); | |
Destroy(this); | |
} | |
IEnumerator fadeCoroutine() { | |
if (Mode == ModeSet.FadeIn) { | |
startVolume = 0; | |
endVolume = MaxVolume; | |
} else if (Mode == ModeSet.FadeOut) { | |
startVolume = MaxVolume; | |
endVolume = 0; | |
} | |
while (elapsedTime <= FadeDuration) { | |
elapsedTime += Time.deltaTime; | |
float volume = Mathf.Lerp(startVolume, endVolume, elapsedTime / FadeDuration); | |
SoundManager.SetBGMMixerVolume(volume); | |
yield return null; | |
} | |
Destroy(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment