You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
8.9 KiB
292 lines
8.9 KiB
//using Luxgen;
|
|
//using System;
|
|
//using System.Collections;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
//using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
[ExecuteInEditMode]
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
public AudioMixer mixer;
|
|
public float minimumTriggingPeriod = 0.2f;
|
|
[Header("Volume (dB)")]
|
|
[Range(-80.0f, 20.0f)]
|
|
public float masterVolume = 0.0f;
|
|
[Range(-80.0f, 20.0f)]
|
|
public float audioVolume = 0.0f;
|
|
[Range(-80.0f, 20.0f)]
|
|
public float effectVolume = 0.0f;
|
|
|
|
public static AudioManager Instance
|
|
{
|
|
get
|
|
{
|
|
_instance = FindObjectOfType(typeof(AudioManager)) as AudioManager;
|
|
if (_instance == null)
|
|
{
|
|
GameObject singleton = new GameObject();
|
|
_instance = singleton.AddComponent<AudioManager>();
|
|
singleton.name = "(singleton) " + typeof(AudioManager).ToString();
|
|
DontDestroyOnLoad(singleton);
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
private static AudioManager _instance;
|
|
private Dictionary<AudioSource, float> soundStamps = new Dictionary<AudioSource, float>();
|
|
private List<GameObject> removal_list = new List<GameObject>();
|
|
|
|
public Transform audioPool { get { return _audioPool; } }
|
|
public Transform effectAudioPool { get { return _effectAudioPool; } }
|
|
public Transform effectAudio { get { return _effectAudio; } }
|
|
private Transform _audioPool;
|
|
private Transform _effectAudioPool;
|
|
private Transform _effectAudio;
|
|
|
|
//private Dictionary<AudioSource, float> audioVolumes = new Dictionary<AudioSource, float>();
|
|
private AudioMixerGroup preEffectAudioGroup;
|
|
private AudioMixerGroup preAudioGroup;
|
|
|
|
Dictionary<AudioSource, Coroutine> SoundfadeRountines = new Dictionary<AudioSource, Coroutine>();
|
|
|
|
public void Trigger(AudioSource sound)
|
|
{
|
|
if (sound == null || sound.transform.parent != effectAudioPool)
|
|
return;
|
|
if (soundStamps.ContainsKey(sound) == false)
|
|
{
|
|
soundStamps.Add(sound, 0);
|
|
}
|
|
if (Time.time - soundStamps[sound] > minimumTriggingPeriod)
|
|
{
|
|
GameObject obj = Instantiate(sound.gameObject, _effectAudio);
|
|
AudioSource audio = obj.GetComponent<AudioSource>();
|
|
audio.loop = false;
|
|
audio.Play();
|
|
soundStamps[sound] = Time.time;
|
|
}
|
|
}
|
|
public void Trigger(string audioName)
|
|
{
|
|
try
|
|
{
|
|
Trigger(_effectAudioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Debug.Log("[AudioManager] Trigger: " + audioName);
|
|
}
|
|
}
|
|
|
|
public void FadeIn(string audioName)
|
|
{
|
|
if(_audioPool.Find(audioName).GetComponent<AudioSource>() != null)
|
|
FadeIn(_audioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
public void FadeIn(AudioSource sound)
|
|
{
|
|
if (sound == null)
|
|
return;
|
|
if (sound.transform.parent == null)
|
|
return;
|
|
if (sound.transform.parent != audioPool)
|
|
return;
|
|
|
|
if (SoundfadeRountines.ContainsKey(sound))
|
|
{
|
|
Coroutine routine = SoundfadeRountines[sound];
|
|
SoundfadeRountines.Remove(sound);
|
|
StopCoroutine(routine);
|
|
}
|
|
|
|
if (sound.volume != 1)
|
|
SoundfadeRountines.Add(sound,StartCoroutine(DoFadeIn(sound)));
|
|
if(!sound.isPlaying)
|
|
sound.Play();
|
|
}
|
|
|
|
public void Play(AudioSource sound)
|
|
{
|
|
if (sound.transform.parent != audioPool)
|
|
return;
|
|
if (sound.isPlaying)
|
|
return;
|
|
sound.Play();
|
|
}
|
|
public void Play(string audioName)
|
|
{
|
|
Play(_effectAudioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
public void Stop(AudioSource sound)
|
|
{
|
|
if (sound.transform.parent != audioPool)
|
|
return;
|
|
|
|
sound.Stop();
|
|
}
|
|
public void Stop(string audioName)
|
|
{
|
|
Stop(_effectAudioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
public void Pause(AudioSource sound)
|
|
{
|
|
if (sound.transform.parent != audioPool)
|
|
return;
|
|
|
|
if (!sound.isPlaying)
|
|
return;
|
|
sound.Pause();
|
|
}
|
|
public void Pause(string audioName)
|
|
{
|
|
Pause(_effectAudioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
|
|
public void FadeOut(string audioName)
|
|
{
|
|
if (_audioPool.Find(audioName).GetComponent<AudioSource>() != null)
|
|
FadeOut(_audioPool.Find(audioName).GetComponent<AudioSource>());
|
|
}
|
|
public void FadeOut(AudioSource sound)
|
|
{
|
|
if (sound == null)
|
|
return;
|
|
if (sound.transform.parent == null)
|
|
return;
|
|
if (sound.transform.parent != audioPool)
|
|
return;
|
|
if (!sound.isPlaying)
|
|
return;
|
|
|
|
if (SoundfadeRountines.ContainsKey(sound))
|
|
{
|
|
Coroutine routine = SoundfadeRountines[sound];
|
|
SoundfadeRountines.Remove(sound);
|
|
StopCoroutine(routine);
|
|
}
|
|
if(sound.volume != 0)
|
|
SoundfadeRountines.Add(sound, StartCoroutine(DoFadeOut(sound)));
|
|
else
|
|
sound.Stop();
|
|
}
|
|
|
|
IEnumerator DoFadeOut(AudioSource audio)
|
|
{
|
|
float volume = audio.volume;
|
|
while(volume > 0)
|
|
{
|
|
volume = Mathf.Clamp(volume-Time.deltaTime, 0,1);
|
|
audio.volume = volume;
|
|
yield return null;
|
|
}
|
|
audio.Stop();
|
|
if (SoundfadeRountines.ContainsKey(audio))
|
|
SoundfadeRountines.Remove(audio);
|
|
}
|
|
IEnumerator DoFadeIn(AudioSource audio)
|
|
{
|
|
audio.Play();
|
|
float volume = audio.volume;
|
|
while (volume < 1)
|
|
{
|
|
volume = Mathf.Clamp(volume + Time.deltaTime, 0, 1);
|
|
audio.volume = volume;
|
|
yield return null;
|
|
}
|
|
if (SoundfadeRountines.ContainsKey(audio))
|
|
SoundfadeRountines.Remove(audio);
|
|
}
|
|
// Use this for initialization
|
|
void Awake () {
|
|
if (_audioPool == null)
|
|
_audioPool = GetChild("Audio Pool");
|
|
if (_effectAudioPool == null)
|
|
_effectAudioPool = GetChild("Effect Audio Pool");
|
|
if (_effectAudio == null)
|
|
_effectAudio = GetChild("Effect Audio");
|
|
}
|
|
Transform GetChild(string name)
|
|
{
|
|
Transform result = transform.Find(name);
|
|
if (result == null)
|
|
{
|
|
GameObject obj = new GameObject(name);
|
|
obj.transform.SetParent(gameObject.transform);
|
|
result = obj.transform;
|
|
}
|
|
return result;
|
|
}
|
|
private AudioMixerGroup GetMixierGroup(string name)
|
|
{
|
|
AudioMixerGroup[] groups = mixer.FindMatchingGroups("Master/" + name);
|
|
if (groups.Length > 0)
|
|
return groups[0];
|
|
return null;
|
|
}
|
|
private void ApplyGroup(Transform trans, AudioMixerGroup group)
|
|
{
|
|
foreach (Transform t in trans)
|
|
{
|
|
AudioSource audio = t.GetComponent<AudioSource>();
|
|
if (audio != null)
|
|
audio.outputAudioMixerGroup = group;
|
|
}
|
|
}
|
|
//float volume = 0;
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
//AudioSource[] _audios = (AudioSource[])FindObjectsOfType(typeof(AudioSource));
|
|
|
|
if (_audioPool == null)
|
|
_audioPool = GetChild("Audio Pool");
|
|
if (_effectAudioPool == null)
|
|
_effectAudioPool = GetChild("Effect Audio Pool");
|
|
if (_effectAudio == null)
|
|
_effectAudio = GetChild("Effect Audio");
|
|
|
|
foreach (Transform t in _effectAudio)
|
|
{
|
|
AudioSource audio = t.GetComponent<AudioSource>();
|
|
if (audio.isPlaying == false)
|
|
removal_list.Add(t.gameObject);
|
|
}
|
|
foreach(GameObject obj in removal_list)
|
|
Destroy(obj);
|
|
removal_list.Clear();
|
|
|
|
|
|
if (mixer != null)
|
|
{
|
|
mixer.SetFloat("Master", masterVolume);
|
|
mixer.SetFloat(_effectAudioPool.name, effectVolume);
|
|
mixer.SetFloat(_audioPool.name, audioVolume);
|
|
|
|
AudioMixerGroup audioGroup = GetMixierGroup(_audioPool.name);
|
|
if (audioGroup != preAudioGroup)
|
|
{
|
|
preAudioGroup = audioGroup;
|
|
ApplyGroup(_audioPool, audioGroup);
|
|
}
|
|
|
|
AudioMixerGroup effectAudioGroup = GetMixierGroup(_effectAudioPool.name);
|
|
if (effectAudioGroup != preEffectAudioGroup)
|
|
{
|
|
preEffectAudioGroup = effectAudioGroup;
|
|
ApplyGroup(_effectAudioPool, effectAudioGroup);
|
|
}
|
|
}
|
|
|
|
/*
|
|
float targetVol = LuxgenCore.Instance.isVideoPlaying()?0.0f: 1.0f;
|
|
volume += (targetVol - volume) * Mathf.Min(1.0f, Time.deltaTime / 0.5f);
|
|
volume = Mathf.Clamp01(volume);
|
|
backgroundMusic.volume = volume;
|
|
*/
|
|
}
|
|
|
|
}
|
|
|