From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/FMOD/XFmod.cs | 453 ++++++++++++++++++++++++ Client/Assets/Scripts/FMOD/XFmod.cs.meta | 8 + Client/Assets/Scripts/FMOD/XFmodBus.cs | 97 +++++ Client/Assets/Scripts/FMOD/XFmodBus.cs.meta | 8 + Client/Assets/Scripts/FMOD/XFmodUIEvent.cs | 46 +++ Client/Assets/Scripts/FMOD/XFmodUIEvent.cs.meta | 8 + 6 files changed, 620 insertions(+) create mode 100644 Client/Assets/Scripts/FMOD/XFmod.cs create mode 100644 Client/Assets/Scripts/FMOD/XFmod.cs.meta create mode 100644 Client/Assets/Scripts/FMOD/XFmodBus.cs create mode 100644 Client/Assets/Scripts/FMOD/XFmodBus.cs.meta create mode 100644 Client/Assets/Scripts/FMOD/XFmodUIEvent.cs create mode 100644 Client/Assets/Scripts/FMOD/XFmodUIEvent.cs.meta (limited to 'Client/Assets/Scripts/FMOD') diff --git a/Client/Assets/Scripts/FMOD/XFmod.cs b/Client/Assets/Scripts/FMOD/XFmod.cs new file mode 100644 index 00000000..6084041d --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmod.cs @@ -0,0 +1,453 @@ +using UnityEngine; +using System.Collections; +using XUtliPoolLib; +using FMODUnity; +using System.Collections.Generic; + +public class XFmod : MonoBehaviour ,IXFmod +{ + private FMODUnity.StudioEventEmitter _emitter = null; + private FMODUnity.StudioEventEmitter _emitter2 = null; + private FMODUnity.StudioEventEmitter _emitter3 = null; + private FMODUnity.StudioEventEmitter _emitter4 = null; + private FMODUnity.StudioEventEmitter _emitter5 = null; + + private Vector3 _3dPos = Vector3.zero; + + public void Destroy() + { + if (_emitter != null) + { + _emitter.Stop(); + _emitter = null; + } + + if (_emitter2 != null) + { + _emitter2.Stop(); + _emitter2 = null; + } + + if (_emitter3 != null) + { + _emitter3.Stop(); + _emitter3 = null; + } + + if (_emitter4 != null) + { + _emitter4.Stop(); + _emitter4 = null; + } + } + + public bool IsPlaying(AudioChannel channel) + { + FMODUnity.StudioEventEmitter e = GetEmitter(channel); + + return e!=null && !e.IsPlaying(); + } + + public void StartEvent(string key, AudioChannel channel = AudioChannel.Action, bool stopPre = true, string para = "", float value = 0) + { + FMODUnity.StudioEventEmitter e = GetEmitter(channel); + if (e == null) + return; + + if (stopPre) e.Stop(); + + if (!string.IsNullOrEmpty(key)) + { + e.Event = key; + } + + e.CacheEventInstance(); + + SetParamValue(channel, para, value); + + //if (_3dPos != Vector3.zero) + //{ + // e.Update3DAttributes(_3dPos); + // _3dPos = Vector3.zero; + //} + + e.Play(); + } + + public void Play(AudioChannel channel = AudioChannel.Action) + { + FMODUnity.StudioEventEmitter e = GetEmitter(channel); + + if (e != null) + e.Play(); + } + + public void Stop(AudioChannel channel = AudioChannel.Action) + { + FMODUnity.StudioEventEmitter e = GetEmitter(channel); + + if (e != null) + e.Stop(); + } + + public void PlayOneShot(string key, Vector3 pos) + { + RuntimeManager.PlayOneShot(key, pos); + } + + public void Update3DAttributes(Vector3 vec, AudioChannel channel = AudioChannel.Action) + { + _3dPos = vec; + } + + public void SetParamValue(AudioChannel channel, string param, float value) + { + if (!string.IsNullOrEmpty(param)) + { + FMODUnity.StudioEventEmitter e = GetEmitter(channel); + if(e!=null) + { + FMODUnity.ParamRef fmodParam = new ParamRef(); + fmodParam.Name = param; + fmodParam.Value = value; + + if (e.Params == null) + { + e.Params = new ParamRef[1]; + e.Params[0].Name = param; + e.Params[0].Value = value; + } + } + + } + } + + public FMODUnity.StudioEventEmitter GetEmitter(AudioChannel channel) + { +#if !DISABLE_FMODE + switch (channel) + { + case AudioChannel.Action: + { + if (_emitter == null) + { + _emitter = gameObject.AddComponent(); + _emitter.StopEvent = EmitterGameEvent.ObjectDestroy; + } + + return _emitter; + } + case AudioChannel.Motion: + { + if (_emitter2 == null) + { + _emitter2 = gameObject.AddComponent(); + _emitter2.StopEvent = EmitterGameEvent.ObjectDestroy; + } + + return _emitter2; + } + case AudioChannel.Skill: + { + if (_emitter3 == null) + { + _emitter3 = gameObject.AddComponent(); + _emitter3.StopEvent = EmitterGameEvent.ObjectDestroy; + } + + return _emitter3; + } + case AudioChannel.Behit: + { + if (_emitter4 == null) + { + _emitter4 = gameObject.AddComponent(); + _emitter4.StopEvent = EmitterGameEvent.ObjectDestroy; + } + + return _emitter4; + } + case AudioChannel.SkillCombine: + { + if (_emitter5 == null) + { + _emitter5 = gameObject.AddComponent(); + //_emitter5.StopEvent = EmitterGameEvent.ObjectDestroy; + } + + return _emitter5; + } + + } + +#endif + return null; + + } + public void Init(GameObject go, Rigidbody rigidbody) + { + } + public bool Deprecated + { + get; + set; + } +} + + + +public class XRuntimeFmod : IXFmod +{ + private static Queue emitterQueue = new Queue(); + private static Queue fmodQueue = new Queue(); + + private RuntimeStudioEventEmitter _emitter = null; + private RuntimeStudioEventEmitter _emitter2 = null; + private RuntimeStudioEventEmitter _emitter3 = null; + private RuntimeStudioEventEmitter _emitter4 = null; + private RuntimeStudioEventEmitter _emitter5 = null; + + private Vector3 _3dPos = Vector3.zero; + public GameObject cachedGo; + public Rigidbody cachedRigidBody; + public static void Clear() + { + emitterQueue.Clear(); + fmodQueue.Clear(); + } + public static XRuntimeFmod GetFMOD() + { + if (fmodQueue.Count > 0) + { + return fmodQueue.Dequeue(); + } + return new XRuntimeFmod(); + } + + public static void ReturnFMOD(XRuntimeFmod fmod) + { + fmodQueue.Enqueue(fmod); + } + private static RuntimeStudioEventEmitter GetEmitter() + { + if (emitterQueue.Count > 0) + { + RuntimeStudioEventEmitter e = emitterQueue.Dequeue(); + e.Reset(); + return e; + } + return new RuntimeStudioEventEmitter(); + } + + private static void ReturnEmitter(RuntimeStudioEventEmitter e) + { + emitterQueue.Enqueue(e); + } + public void Init(GameObject go, Rigidbody rigidbody) + { + cachedGo = go; + cachedRigidBody = rigidbody; + //if (_emitter != null) + //{ + // _emitter.Set3DPos(cachedGo, cachedRigidBody); + //} + + //if (_emitter2 != null) + //{ + // _emitter2.Set3DPos(cachedGo, cachedRigidBody); + //} + + //if (_emitter3 != null) + //{ + // _emitter3.Set3DPos(cachedGo, cachedRigidBody); + //} + + //if (_emitter4 != null) + //{ + // _emitter4.Set3DPos(cachedGo, cachedRigidBody); + //} + + //if (_emitter5 != null) + //{ + // _emitter5.Set3DPos(cachedGo, cachedRigidBody); + //} + } + + public void Destroy() + { + if (_emitter != null) + { + _emitter.Stop(); + ReturnEmitter(_emitter); + _emitter = null; + } + + if (_emitter2 != null) + { + _emitter2.Stop(); + ReturnEmitter(_emitter2); + _emitter2 = null; + } + + if (_emitter3 != null) + { + _emitter3.Stop(); + ReturnEmitter(_emitter3); + _emitter3 = null; + } + + if (_emitter4 != null) + { + _emitter4.Stop(); + ReturnEmitter(_emitter4); + _emitter4 = null; + } + + if (_emitter5 != null) + { + _emitter5.Stop(); + ReturnEmitter(_emitter5); + _emitter5 = null; + } + } + + public bool IsPlaying(AudioChannel channel) + { + RuntimeStudioEventEmitter e = GetEmitter(channel); + + return e != null && !e.IsPlaying(); + } + + public void StartEvent(string key, AudioChannel channel = AudioChannel.Action, bool stopPre = true, string para = "", float value = 0) + { + RuntimeStudioEventEmitter e = GetEmitter(channel); + if (e == null) + return; + if (stopPre) e.Stop(); + + if (!string.IsNullOrEmpty(key)) + { + e.Event = key; + } + + e.CacheEventInstance(); + + SetParamValue(e, para, value); + + if (_3dPos != Vector3.zero) + { + e.Update3DAttributes(_3dPos); + _3dPos = Vector3.zero; + } + + e.Play(cachedGo, cachedRigidBody); + } + + public void Play(AudioChannel channel = AudioChannel.Action) + { + RuntimeStudioEventEmitter e = GetEmitter(channel); + + if (e != null) + e.Play(cachedGo, cachedRigidBody); + } + + public void Stop(AudioChannel channel = AudioChannel.Action) + { + RuntimeStudioEventEmitter e = GetEmitter(channel); + + if (e != null) + e.Stop(); + } + + public void PlayOneShot(string key, Vector3 pos) + { +#if !DISABLE_FMODE + RuntimeManager.PlayOneShot(key, pos); +#endif + } + + public void Update3DAttributes(Vector3 vec, AudioChannel channel = AudioChannel.Action) + { + _3dPos = vec; + } + + public void SetParamValue(RuntimeStudioEventEmitter e, string param, float value) + { + if (!string.IsNullOrEmpty(param)) + { + FMODUnity.ParamRef fmodParam = new ParamRef(); + fmodParam.Name = param; + fmodParam.Value = value; + + if (e.Params == null) + { + e.Params = new List(); + e.Params.Add(fmodParam); + } + } + } + + private RuntimeStudioEventEmitter GetEmitter(AudioChannel channel) + { +#if !DISABLE_FMODE + switch (channel) + { + case AudioChannel.Action: + { + if (_emitter == null) + { + _emitter = GetEmitter(); + } + + return _emitter; + } + case AudioChannel.Motion: + { + if (_emitter2 == null) + { + _emitter2 = GetEmitter(); + } + + return _emitter2; + } + case AudioChannel.Skill: + { + if (_emitter3 == null) + { + _emitter3 = GetEmitter(); + } + + return _emitter3; + } + case AudioChannel.Behit: + { + if (_emitter4 == null) + { + _emitter4 = GetEmitter(); + } + + return _emitter4; + } + case AudioChannel.SkillCombine: + { + if (_emitter5 == null) + { + _emitter5 = GetEmitter(); + } + + return _emitter5; + } + } +#endif + return null; + + } + + public bool Deprecated + { + get; + set; + } +} + + diff --git a/Client/Assets/Scripts/FMOD/XFmod.cs.meta b/Client/Assets/Scripts/FMOD/XFmod.cs.meta new file mode 100644 index 00000000..dd573450 --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmod.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 76bc3b87d93e19040886fb93eb6429ae +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Client/Assets/Scripts/FMOD/XFmodBus.cs b/Client/Assets/Scripts/FMOD/XFmodBus.cs new file mode 100644 index 00000000..b582ac6c --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmodBus.cs @@ -0,0 +1,97 @@ +using UnityEngine; +using XUtliPoolLib; + +public class XFmodBus : MonoBehaviour, IXFmodBus +{ + FMOD.Studio.Bus bus; + FMOD.Studio.VCA mainVCA; + FMOD.Studio.VCA bgmVCA; + FMOD.Studio.VCA sfxVCA; + FMODUnity.StudioEventEmitter e; + + //public void SetMute(bool mute) + //{ + // GetBus(); + // bus.setMute(mute); + //} + + public void SetBusVolume(string strBus, float volume) + { + FMOD.Studio.Bus bus; + FMODUnity.RuntimeManager.StudioSystem.getBus(strBus, out bus); + + if (bus != null) bus.setVolume(volume); + } + + //public void SetFaderLevel(float volume) + //{ + // GetBus(); + // bus.setFaderLevel(volume); + //} + + //public float GetFaderLevel() + //{ + // GetBus(); + // float f = 0; + // bus.getFaderLevel(out f); + // return f; + //} + + //protected void GetBus() + //{ + // if (bus == null) + // bus = FMOD_StudioSystem.instance.GetBus("bus:/"); + //} + + public void SetMainVolume(float volume) + { + if (mainVCA == null) + FMODUnity.RuntimeManager.StudioSystem.getVCA("vca:/Main Volume Control", out mainVCA); + + if (mainVCA != null) mainVCA.setVolume(volume); + } + + public void SetBGMVolume(float volume) + { + if (bgmVCA == null) + FMODUnity.RuntimeManager.StudioSystem.getVCA("vca:/BGM Volume Control", out bgmVCA); + + if (bgmVCA != null) bgmVCA.setVolume(volume); + } + + public void SetSFXVolume(float volume) + { + if (sfxVCA == null) + FMODUnity.RuntimeManager.StudioSystem.getVCA("vca:/SFX Volume Control", out sfxVCA); + + if (sfxVCA != null) sfxVCA.setVolume(volume); + } + + public void PlayOneShot(string key, Vector3 pos) + { + FMODUnity.RuntimeManager.PlayOneShot(key, pos); + } + + public void StartEvent(string key) + { + if (e == null) + e = gameObject.AddComponent(); + + e.Event = key; + e.CacheEventInstance(); + e.Play(); + } + + public void StopEvent() + { + if (e == null) return; + + e.Stop(); + } + + public bool Deprecated + { + get; + set; + } +} diff --git a/Client/Assets/Scripts/FMOD/XFmodBus.cs.meta b/Client/Assets/Scripts/FMOD/XFmodBus.cs.meta new file mode 100644 index 00000000..b80b314a --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmodBus.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6a8e0fc5e1644914a9128620f4b94137 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs b/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs new file mode 100644 index 00000000..984a6ee2 --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs @@ -0,0 +1,46 @@ +using UnityEngine; +using System.Collections; + +public class XFmodUIEvent : MonoBehaviour { + + static private GameObject _ui_audio = null; + + public string Name = ""; + public float Delay = 0; + private float _start_time; + void Start () { + if (_ui_audio == null) + { + if (GameObject.Find("UIRoot") != null) + _ui_audio = GameObject.Find("UIRoot").gameObject; + else + _ui_audio = GameObject.Find("UIRoot(Clone)").gameObject; + } + _start_time = UnityEngine.Time.time; + } + + void FixedUpdate () { + + if (UnityEngine.Time.time - _start_time > Delay) + { + XFmod iFmod; + if (_ui_audio != null) + { + iFmod = GetFmodComponent(_ui_audio); + iFmod.PlayOneShot("event:/" + Name, Vector3.zero); + } + + Destroy(this); + } + } + + public XFmod GetFmodComponent(GameObject go) + { + XFmod iFmod = go.GetComponent(); + + if (iFmod == null) + iFmod = go.AddComponent(); + + return iFmod; + } +} diff --git a/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs.meta b/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs.meta new file mode 100644 index 00000000..9d244a10 --- /dev/null +++ b/Client/Assets/Scripts/FMOD/XFmodUIEvent.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8114777d87bd3af4ebb99fe2ac50f27b +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: -- cgit v1.1-26-g67d0