using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioController : MonoBehaviour { public Transform objectA; public Transform objectB; 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); NotificationServer.register("statechange Searchlight", searchlightStateChanged); } 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.4"); } // void Update() // { // doMusicMix(); // } public void doMusicMix() { NotificationServer.notify("play bgm", "tense:0.75"); // float distance = Vector3.Distance(objectA.position, objectB.position); // Debug.LogWarning(distance); // distance = Mathf.Clamp(1000f / distance, 0f, 1f); // float tenseVol = 0.75f * distance; // float spottedVol = 0.5f * (1f-distance); // NotificationServer.notify("play bgm", "tense:"+tenseVol+"/spotted"+spottedVol); } public void searchlightStateChanged() { if (Searchlight.state == Searchlight.SearchState.Chasing) { NotificationServer.notify("play sfx", "spottedDialogue:0.9"); NotificationServer.notify("play sfx", "shipSpotted:0.5"); // NotificationServer.notify("play sfx", "spottedDialogue"); NotificationServer.notify("play bgm", "attack:9"); } else { doMusicMix(); } } 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, 1f).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, 1f).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(); return; } } } 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; int timeSamples = 0; foreach (AudioSource s in bgmSources) { if (s.clip != null && s.clip == clip) { LeanTween.cancel(s.gameObject); foundEmpty = s; } else if (s.isPlaying) { timeSamples = s.timeSamples; LeanTween.cancel(s.gameObject); LeanTween.value(s.gameObject, s.volume, 0f, 1f).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; if (timeSamples < foundEmpty.clip.samples) foundEmpty.timeSamples = timeSamples; else foundEmpty.timeSamples = 0; foundEmpty.Play(); LeanTween.value(foundEmpty.gameObject, foundEmpty.volume, volume, 1f).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, 1f).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, 1f).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, 1f).setOnUpdate((float val)=>{ s.volume = val; }).setOnComplete(()=>{ s.Stop(); s.clip = null; }).setIgnoreTimeScale(true); } } } }