play audio via FMOD using key from dialogue database

This commit is contained in:
Cat Flynn 2021-02-22 15:29:17 +00:00
parent d0cfd38119
commit 60634152d3
5 changed files with 67 additions and 20 deletions

View File

@ -13,3 +13,4 @@ MonoBehaviour:
m_Name: Dialogue Settings
m_EditorClassIdentifier:
_hideAfter: 5
_fmodKeyPrefix: event:/Character/

View File

@ -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

View File

@ -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<string, string> _dict = new Dictionary<string, string>();
public static string[] Keys => _dict.Keys.ToArray();
public static string ReadDialogue(string key)
{
return _dict.ContainsKey(key)

View File

@ -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;
}

View File

@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CreateAssetMenu(menuName = "KernelPanic/Dialogue/Dialogue System")]
@ -13,16 +14,50 @@ public partial class DialogueSystem : ScriptableObject
// https://stackoverflow.com/questions/2282476/actiont-vs-delegate-event
public event EventHandler<DialogueLine> onDialogueLine;
private readonly Dictionary<string, string> _fmodKeyCache = new Dictionary<string, string>();
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;
onDialogueLine?.Invoke(this, dl);
// read audio data out of FMOD, check if event exists
if (eventDescription.isValid())
{
// assign values and play audio
// TODO: tell FMOD to play the key
// 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);
}
}
@ -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