From 60634152d346752c995ead9c16f9e14379426921 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Mon, 22 Feb 2021 15:29:17 +0000 Subject: [PATCH] play audio via FMOD using key from dialogue database --- .../Data/Dialogue/Dialogue Settings.asset | 1 + game/Assets/Prefabs/Player.prefab | 24 +++++---- .../Scripts/Dialogue/DialogueDatabase.cs | 3 ++ .../Scripts/Dialogue/DialogueSettings.cs | 9 ++-- .../Assets/Scripts/Dialogue/DialogueSystem.cs | 50 ++++++++++++++++--- 5 files changed, 67 insertions(+), 20 deletions(-) diff --git a/game/Assets/Data/Dialogue/Dialogue Settings.asset b/game/Assets/Data/Dialogue/Dialogue Settings.asset index a9183a3..2012420 100644 --- a/game/Assets/Data/Dialogue/Dialogue Settings.asset +++ b/game/Assets/Data/Dialogue/Dialogue Settings.asset @@ -13,3 +13,4 @@ MonoBehaviour: m_Name: Dialogue Settings m_EditorClassIdentifier: _hideAfter: 5 + _fmodKeyPrefix: event:/Character/ diff --git a/game/Assets/Prefabs/Player.prefab b/game/Assets/Prefabs/Player.prefab index 227982f..69f9169 100644 --- a/game/Assets/Prefabs/Player.prefab +++ b/game/Assets/Prefabs/Player.prefab @@ -323,9 +323,9 @@ GameObject: m_Component: - component: {fileID: 13726837293638830} - component: {fileID: 13726837293638818} - - component: {fileID: 13726837293638819} - component: {fileID: 13726837293638816} - component: {fileID: 13726837293638817} + - component: {fileID: 9057303937983535475} m_Layer: 0 m_Name: Camera m_TagString: Untagged @@ -390,14 +390,6 @@ Camera: m_OcclusionCulling: 1 m_StereoConvergence: 10 m_StereoSeparation: 0.022 ---- !u!81 &13726837293638819 -AudioListener: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 13726837293638831} - m_Enabled: 1 --- !u!114 &13726837293638816 MonoBehaviour: m_ObjectHideFlags: 0 @@ -444,6 +436,20 @@ MonoBehaviour: m_EditorClassIdentifier: _settings: {fileID: 11400000, guid: 995f378ab762cd344b7a6d108f049191, type: 2} _inputHandler: {fileID: 13726836969441782} +--- !u!114 &9057303937983535475 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 13726837293638831} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 86c6556701af9e04380698b89f691b6e, type: 3} + m_Name: + m_EditorClassIdentifier: + attenuationObject: {fileID: 0} + ListenerNumber: -1 --- !u!1 &13726837642651461 GameObject: m_ObjectHideFlags: 0 diff --git a/game/Assets/Scripts/Dialogue/DialogueDatabase.cs b/game/Assets/Scripts/Dialogue/DialogueDatabase.cs index 256964c..b127959 100644 --- a/game/Assets/Scripts/Dialogue/DialogueDatabase.cs +++ b/game/Assets/Scripts/Dialogue/DialogueDatabase.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; @@ -139,6 +140,8 @@ public static partial class DialogueDatabase private static readonly Dictionary _dict = new Dictionary(); + public static string[] Keys => _dict.Keys.ToArray(); + public static string ReadDialogue(string key) { return _dict.ContainsKey(key) diff --git a/game/Assets/Scripts/Dialogue/DialogueSettings.cs b/game/Assets/Scripts/Dialogue/DialogueSettings.cs index b681422..0503f0a 100644 --- a/game/Assets/Scripts/Dialogue/DialogueSettings.cs +++ b/game/Assets/Scripts/Dialogue/DialogueSettings.cs @@ -6,7 +6,8 @@ using UnityEngine; public class DialogueSettings : ScriptableObject { public float HideAfter => _hideAfter; - - [SerializeField] - private float _hideAfter; -} + [SerializeField] private float _hideAfter; + + public string FMODPrefix => _fmodKeyPrefix; + [SerializeField] private string _fmodKeyPrefix; +} \ No newline at end of file diff --git a/game/Assets/Scripts/Dialogue/DialogueSystem.cs b/game/Assets/Scripts/Dialogue/DialogueSystem.cs index 2287db3..c1ea9b2 100644 --- a/game/Assets/Scripts/Dialogue/DialogueSystem.cs +++ b/game/Assets/Scripts/Dialogue/DialogueSystem.cs @@ -1,28 +1,63 @@ using System; +using System.Collections.Generic; using UnityEngine; - #if UNITY_EDITOR using UnityEditor; + #endif [CreateAssetMenu(menuName = "KernelPanic/Dialogue/Dialogue System")] public partial class DialogueSystem : ScriptableObject { [SerializeField] private DialogueSettings _settings; - + // https://stackoverflow.com/questions/2282476/actiont-vs-delegate-event public event EventHandler onDialogueLine; + private readonly Dictionary _fmodKeyCache = new Dictionary(); + + private void OnEnable() + { + // cache all dialogue keys for FMOD at start to avoid allocations later + foreach (var key in DialogueDatabase.Keys) + { + _fmodKeyCache[key] = $"{_settings.FMODPrefix}{key}"; + } + } + public void PlayLine(string key) { + // retrieve cached key + var fmodKey = _fmodKeyCache[key]; + var eventDescription = FMODUnity.RuntimeManager.GetEventDescription(fmodKey); + DialogueLine dl; dl.text = DialogueDatabase.ReadDialogue(key); - // TODO: get dialogue line duration from FMOD + // default duration to show ui elements for dl.duration = _settings.HideAfter; - + + // read audio data out of FMOD, check if event exists + if (eventDescription.isValid()) + { + // assign values and play audio + + // get dialogue line duration from FMOD + eventDescription.getLength(out int ms); + + // get length gives us a value in milliseconds so it needs to be converted to seconds + // before assignment + dl.duration = ms / 1000f; + + // event is valid + FMODUnity.RuntimeManager.PlayOneShot(fmodKey); + } + else + { + // no event available boooooooo + Debug.LogError($"FMOD event desc for key {fmodKey} is not valid", this); + } + onDialogueLine?.Invoke(this, dl); - - // TODO: tell FMOD to play the key } } @@ -33,6 +68,7 @@ public struct DialogueLine } #region Editor + #if UNITY_EDITOR [CustomEditor(typeof(DialogueSystem))] @@ -60,5 +96,5 @@ public class DialogueSystemEditor : Editor } #endif -#endregion +#endregion \ No newline at end of file