|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using System.IO;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using Unity.VisualScripting;
|
|
|
|
|
|
|
|
public class NarrationManager : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Singleton Implementation //
|
|
|
|
public static NarrationManager _instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
// Variables //
|
|
|
|
private List<string> m_scriptSlices = new List<string>();
|
|
|
|
|
|
|
|
public TextAsset m_Filename;
|
|
|
|
|
|
|
|
private string m_UnprocessedFile;
|
|
|
|
|
|
|
|
private int m_ScriptIndex;
|
|
|
|
|
|
|
|
private float m_SubtitileWaitTime = 8;
|
|
|
|
|
|
|
|
// Unity Events //
|
|
|
|
public delegate void onUpdateSubtitileDelegate(string subtitle, float waitTime);
|
|
|
|
public static onUpdateSubtitileDelegate m_UpdateSubtitile;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
if (_instance != null)
|
|
|
|
Destroy(this);
|
|
|
|
else
|
|
|
|
_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in a text file, split into sentences, and add to list.
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
if (m_Filename != null)
|
|
|
|
{
|
|
|
|
m_UnprocessedFile = m_Filename.text;
|
|
|
|
ParseSentences();
|
|
|
|
m_ScriptIndex = 0;
|
|
|
|
StartCoroutine(WaitToStart());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Log($"No filename given for script.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator WaitToStart()
|
|
|
|
{
|
|
|
|
yield return new WaitForSecondsRealtime(5f);
|
|
|
|
updateSubtitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coroutine for subtitles, when to start the next one after a delay
|
|
|
|
IEnumerator SubtitleWait()
|
|
|
|
{
|
|
|
|
m_SubtitileWaitTime = Random.Range(12.5f, 20f);
|
|
|
|
yield return new WaitForSeconds(m_SubtitileWaitTime);
|
|
|
|
updateSubtitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CalculateVariableWaitTime(string sentence)
|
|
|
|
{
|
|
|
|
var temp = Mathf.Ceil(sentence.Length / 5);
|
|
|
|
if (temp <= 0)
|
|
|
|
{
|
|
|
|
m_SubtitileWaitTime = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_SubtitileWaitTime = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increments to next index in list, and updates UI.
|
|
|
|
void updateSubtitle()
|
|
|
|
{
|
|
|
|
if (m_ScriptIndex == m_scriptSlices.Count)
|
|
|
|
m_ScriptIndex = 0;
|
|
|
|
CalculateVariableWaitTime(m_scriptSlices[m_ScriptIndex]);
|
|
|
|
DisplayText(m_scriptSlices[m_ScriptIndex]);
|
|
|
|
m_ScriptIndex++;
|
|
|
|
StartCoroutine(SubtitleWait());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invokes a Unity Event which updates UI.
|
|
|
|
private void DisplayText(string slice)
|
|
|
|
{
|
|
|
|
m_UpdateSubtitile?.Invoke(slice, m_SubtitileWaitTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse sentences by newlines
|
|
|
|
private void ParseSentences()
|
|
|
|
{
|
|
|
|
m_scriptSlices.AddRange(
|
|
|
|
m_UnprocessedFile.Split("\n"[0]));
|
|
|
|
Debug.Log($"Parsing of text done, line count: {m_scriptSlices.Count}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|