You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.4 KiB
121 lines
3.4 KiB
10 months ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using System.IO;
|
||
|
using UnityEngine.Events;
|
||
|
using Unity.VisualScripting;
|
||
|
|
||
|
namespace Moloch
|
||
|
{
|
||
|
public class NarrationManager : MonoBehaviour
|
||
|
{
|
||
|
// Singleton Implementation //
|
||
|
private static NarrationManager _instance;
|
||
|
public static NarrationManager Instance
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if( _instance == null)
|
||
|
{
|
||
|
_instance = FindAnyObjectByType<NarrationManager>();
|
||
|
if( _instance != null)
|
||
|
{
|
||
|
_instance = new GameObject().AddComponent<NarrationManager>();
|
||
|
}
|
||
|
}
|
||
|
return _instance;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 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);
|
||
|
DontDestroyOnLoad(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(15f, 45f);
|
||
|
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()
|
||
|
{
|
||
|
m_ScriptIndex++;
|
||
|
if(m_ScriptIndex == m_scriptSlices.Count)
|
||
|
m_ScriptIndex = 0;
|
||
|
CalculateVariableWaitTime(m_scriptSlices[m_ScriptIndex]);
|
||
|
DisplayText(m_scriptSlices[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}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|