2021-02-19 17:33:02 +01:00
|
|
|
using System;
|
2021-02-22 16:29:17 +01:00
|
|
|
using System.Collections.Generic;
|
2021-02-23 14:05:42 +01:00
|
|
|
using FMOD.Studio;
|
2021-02-19 17:33:02 +01:00
|
|
|
using UnityEngine;
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
2021-02-22 16:29:17 +01:00
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
[CreateAssetMenu(menuName = "KernelPanic/Dialogue/Dialogue System")]
|
|
|
|
public partial class DialogueSystem : ScriptableObject
|
|
|
|
{
|
|
|
|
[SerializeField] private DialogueSettings _settings;
|
2021-02-22 16:29:17 +01:00
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
// https://stackoverflow.com/questions/2282476/actiont-vs-delegate-event
|
|
|
|
public event EventHandler<DialogueLine> onDialogueLine;
|
|
|
|
|
2021-02-22 16:29:17 +01:00
|
|
|
private readonly Dictionary<string, string> _fmodKeyCache = new Dictionary<string, string>();
|
2021-03-02 14:24:18 +01:00
|
|
|
// a list of dialogue keys that have already been spoken
|
|
|
|
private readonly List<string> _usedKeys = new List<string>();
|
2021-02-22 16:29:17 +01:00
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
2021-03-02 14:24:18 +01:00
|
|
|
_fmodKeyCache.Clear();
|
|
|
|
_usedKeys.Clear();
|
|
|
|
|
2021-02-22 16:29:17 +01:00
|
|
|
// cache all dialogue keys for FMOD at start to avoid allocations later
|
|
|
|
foreach (var key in DialogueDatabase.Keys)
|
|
|
|
{
|
|
|
|
_fmodKeyCache[key] = $"{_settings.FMODPrefix}{key}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:24:18 +01:00
|
|
|
// noRepeat locks this key off from further use. further attempts to use the key will be discarded
|
|
|
|
public void PlayLine(string key, bool noRepeat = true)
|
2021-02-19 17:33:02 +01:00
|
|
|
{
|
2021-03-02 14:24:18 +01:00
|
|
|
if (noRepeat)
|
|
|
|
{
|
|
|
|
if (_usedKeys.Contains(key)) return;
|
|
|
|
|
|
|
|
_usedKeys.Add(key);
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:29:17 +01:00
|
|
|
// retrieve cached key
|
|
|
|
var fmodKey = _fmodKeyCache[key];
|
2021-02-23 14:05:42 +01:00
|
|
|
|
|
|
|
EventDescription? eventDescription = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
eventDescription = FMODUnity.RuntimeManager.GetEventDescription(fmodKey);
|
|
|
|
}
|
|
|
|
catch (FMODUnity.EventNotFoundException e)
|
|
|
|
{
|
|
|
|
Debug.LogWarning($"no FMOD event {fmodKey}");
|
|
|
|
}
|
2021-02-22 16:29:17 +01:00
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
DialogueLine dl;
|
|
|
|
dl.text = DialogueDatabase.ReadDialogue(key);
|
2021-02-22 16:29:17 +01:00
|
|
|
// default duration to show ui elements for
|
2021-02-19 17:33:02 +01:00
|
|
|
dl.duration = _settings.HideAfter;
|
2021-02-22 16:29:17 +01:00
|
|
|
|
|
|
|
// read audio data out of FMOD, check if event exists
|
2021-02-23 14:05:42 +01:00
|
|
|
if (eventDescription.HasValue)
|
2021-02-22 16:29:17 +01:00
|
|
|
{
|
|
|
|
// assign values and play audio
|
|
|
|
|
|
|
|
// get dialogue line duration from FMOD
|
2021-02-23 14:05:42 +01:00
|
|
|
eventDescription.Value.getLength(out int ms);
|
2021-02-22 16:29:17 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
onDialogueLine?.Invoke(this, dl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct DialogueLine
|
|
|
|
{
|
|
|
|
public string text;
|
|
|
|
public float duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Editor
|
2021-02-22 16:29:17 +01:00
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
|
|
[CustomEditor(typeof(DialogueSystem))]
|
|
|
|
public class DialogueSystemEditor : Editor
|
|
|
|
{
|
|
|
|
private DialogueSystem _dialogue;
|
|
|
|
private string _key;
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
_dialogue = target as DialogueSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnInspectorGUI()
|
|
|
|
{
|
|
|
|
base.OnInspectorGUI();
|
|
|
|
|
|
|
|
_key = EditorGUILayout.TextField("key", _key);
|
|
|
|
|
|
|
|
if (GUILayout.Button("Play Line"))
|
|
|
|
{
|
|
|
|
_dialogue.PlayLine(_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-02-22 16:29:17 +01:00
|
|
|
#endregion
|