using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioController : MonoBehaviour { public AudioSource[] bgmSources; public AudioSource[] ambSources; public AudioSource[] sfxSources; public AudioClip[] bgmClips; public AudioClip[] ambClips; public AudioClip[] sfxClips; void Awake() { NotificationServer.register("play bgm", playBgm); NotificationServer.register("play amb", playAmb); NotificationServer.register("play sfx", playSfx); NotificationServer.register("fade bgm", fadeBgm); NotificationServer.register("fade amb", fadeAmb); } void Start() { foreach (AudioSource s in bgmSources) s.volume = 0f; foreach (AudioSource s in ambSources) s.volume = 0f; foreach (AudioSource s in sfxSources) s.volume = 0f; NotificationServer.notify("play amb", "oceanAmbience:0.5"); } public void playAmb(object audioNameObj) { string audioName = audioNameObj as string; if (audioName == null || audioName == "") return; string[] tokens = audioName.ToLower().Split(':'); audioName = tokens[0]; float volume = 1f; if (tokens.Length > 1) float.TryParse(tokens[1], out volume); AudioClip clip = null; foreach (AudioClip c in ambClips) { if (c.name.ToLower() == audioName) { clip = c; break; } } if (clip == null) return; bool foundEmpty = false; foreach (AudioSource s in ambSources) { if (s.isPlaying) { LeanTween.cancel(s.gameObject); LeanTween.value(s.gameObject, s.volume, 0f, 0.5f).setOnUpdate((float val)=>{ s.volume = val; }).setOnComplete(()=>{ s.Stop(); s.clip = null; }).setIgnoreTimeScale(true); } else if (!foundEmpty) { LeanTween.cancel(s.gameObject); foundEmpty = true; s.clip = clip; s.Play(); LeanTween.value(s.gameObject, s.volume, volume, 0.5f).setOnUpdate((float val)=>{ s.volume = val; }).setIgnoreTimeScale(true); } } } public void playSfx(object audioNameObj) { string audioName = audioNameObj as string; if (audioName == null || audioName == "") return; string[] tokens = audioName.ToLower().Split(':'); audioName = tokens[0]; float volume = 1f; if (tokens.Length > 1) float.TryParse(tokens[1], out volume); AudioClip clip = null; foreach (AudioClip c in sfxClips) { if (c.name.ToLower() == audioName) { clip = c; break; } } if (clip == null) return; foreach (AudioSource s in sfxSources) { if (!s.isPlaying) { LeanTween.cancel(s.gameObject); s.clip = clip; s.volume = volume; s.Play(); } } } public void playBgm(object audioNameObj) { string audioNames = audioNameObj as string; if (audioNames == null || audioNames == "") return; audioNames = audioNames.ToLower(); string[] tokens = audioNames.Split('/'); if (tokens.Length > 1) { foreach (string t in tokens) playBgm(t.Trim()); return; } string[] tokens2 = audioNames.Split(':'); string audioName = tokens2[0]; float volume = 1f; if (tokens2.Length > 1) float.TryParse(tokens2[1], out volume); AudioClip clip = null; foreach (AudioClip c in bgmClips) { if (c.name.ToLower() == audioName) { clip = c; break; } } if (clip == null) return; AudioSource foundEmpty = null; foreach (AudioSource s in bgmSources) { if (s.clip != null && s.clip == clip) { LeanTween.cancel(s.gameObject); foundEmpty = s; } else if (s.isPlaying) { LeanTween.cancel(s.gameObject); LeanTween.value(s.gameObject, s.volume, 0f, 0.5f).setOnUpdate((float val)=>{ s.volume = val; }).setOnComplete(()=>{ s.Stop(); s.clip = null; }).setIgnoreTimeScale(true); } else if (foundEmpty == null) { LeanTween.cancel(s.gameObject); foundEmpty = s; } } if (foundEmpty != null && !foundEmpty.isPlaying) { LeanTween.cancel(foundEmpty.gameObject); foundEmpty.clip = clip; foundEmpty.Play(); LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 0.5f).setOnUpdate((float val)=>{ foundEmpty.volume = val; }).setIgnoreTimeScale(true); } else if (foundEmpty != null && foundEmpty.isPlaying) { LeanTween.cancel(foundEmpty.gameObject); LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 0.5f).setOnUpdate((float val)=>{ foundEmpty.volume = val; }).setIgnoreTimeScale(true); } } public void fadeBgm() { foreach (AudioSource s in bgmSources) { if (s.isPlaying) { LeanTween.cancel(s.gameObject); LeanTween.value(s.gameObject, s.volume, 0f, 0.5f).setOnUpdate((float val)=>{ s.volume = val; }).setOnComplete(()=>{ s.Stop(); s.clip = null; }).setIgnoreTimeScale(true); } } } public void fadeAmb() { foreach (AudioSource s in ambSources) { if (s.isPlaying) { LeanTween.cancel(s.gameObject); LeanTween.value(s.gameObject, s.volume, 0f, 0.5f).setOnUpdate((float val)=>{ s.volume = val; }).setOnComplete(()=>{ s.Stop(); s.clip = null; }).setIgnoreTimeScale(true); } } } }