add project files
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace OHM.UnityToolkit
|
||||
{
|
||||
public class AudioManager : MonoBehaviour
|
||||
{
|
||||
public AudioMixer mixer;
|
||||
|
||||
public AudioListener listener;
|
||||
|
||||
public string sfxVolumeParamName;
|
||||
|
||||
public string musicVolumeParamName;
|
||||
|
||||
private AudioSource _currentMusicSource;
|
||||
|
||||
private IEnumerator _crossFadeCoroutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
}
|
||||
|
||||
public void Setup(bool masterEnabled, bool sfxEnabled, bool musicEnabled)
|
||||
{
|
||||
SetMasterEnabled(masterEnabled);
|
||||
SetGroupEnabled(musicVolumeParamName, musicEnabled);
|
||||
SetGroupEnabled(sfxVolumeParamName, sfxEnabled);
|
||||
}
|
||||
|
||||
public void SetMasterEnabled(bool enabled)
|
||||
{
|
||||
if (listener != null)
|
||||
{
|
||||
listener.enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMusicEnabled(bool enabled)
|
||||
{
|
||||
SetGroupEnabled(musicVolumeParamName, enabled);
|
||||
}
|
||||
|
||||
public void SetSFXEnabled(bool enabled)
|
||||
{
|
||||
SetGroupEnabled(sfxVolumeParamName, enabled);
|
||||
}
|
||||
|
||||
private void SetGroupEnabled(string paramName, bool enabled)
|
||||
{
|
||||
mixer.SetFloat(paramName, enabled ? 0f : -100f);
|
||||
}
|
||||
|
||||
public void StopCurrentMusic()
|
||||
{
|
||||
if (_currentMusicSource != null)
|
||||
{
|
||||
_currentMusicSource.Stop();
|
||||
_currentMusicSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayMusic(AudioSource source)
|
||||
{
|
||||
StopCrossFade();
|
||||
StopCurrentMusic();
|
||||
if (source == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
source.volume = 1f;
|
||||
source.Play();
|
||||
_currentMusicSource = source;
|
||||
}
|
||||
|
||||
private void StopCrossFade()
|
||||
{
|
||||
if (_crossFadeCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_crossFadeCoroutine);
|
||||
_crossFadeCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void CrossFadeMusic(AudioSource newSource, float duration)
|
||||
{
|
||||
StopCrossFade();
|
||||
_crossFadeCoroutine = CrossFadeCoroutine(_currentMusicSource, newSource, duration);
|
||||
_currentMusicSource = newSource;
|
||||
StartCoroutine(_crossFadeCoroutine);
|
||||
}
|
||||
|
||||
private IEnumerator CrossFadeCoroutine(AudioSource prevSource, AudioSource newSource, float duration)
|
||||
{
|
||||
if (newSource != null)
|
||||
{
|
||||
newSource.Play();
|
||||
}
|
||||
for (float t = 0f; t < duration; t += Time.deltaTime)
|
||||
{
|
||||
float ratio = t / duration;
|
||||
if (prevSource != null)
|
||||
{
|
||||
prevSource.volume = 1f - ratio;
|
||||
}
|
||||
if (newSource != null)
|
||||
{
|
||||
newSource.volume = ratio;
|
||||
}
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
if (prevSource != null)
|
||||
{
|
||||
prevSource.Stop();
|
||||
}
|
||||
_crossFadeCoroutine = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user