script added & managers for narration/subtitles
This commit is contained in:
69
Assets/8_Scripts/1_Managers/BodyManager.cs
Normal file
69
Assets/8_Scripts/1_Managers/BodyManager.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
public class BodyManager : MonoBehaviour
|
||||
{
|
||||
public static BodyManager _instance { get; private set; }
|
||||
|
||||
public Body m_Rigidbody;
|
||||
public ObjectPool<Body> m_Pool;
|
||||
public float m_SpawnTime;
|
||||
public int m_DefaultPoolCapacity;
|
||||
public int m_MaxPoolCapacity;
|
||||
public Transform m_Parent;
|
||||
private Vector3 m_PelvisPosition;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null)
|
||||
Destroy(this);
|
||||
else
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (m_Rigidbody == null)
|
||||
Debug.Log($"Rigidbody is null");
|
||||
|
||||
m_Pool = new ObjectPool<Body>(CreateBody, OnTakeBodyFromPool, OnReturnBodyToPool, OnDestroyBody, true, m_DefaultPoolCapacity, m_MaxPoolCapacity);
|
||||
|
||||
StartCoroutine(Spawn());
|
||||
}
|
||||
|
||||
private Body CreateBody()
|
||||
{
|
||||
Vector3 spawnPos = this.transform.position;
|
||||
Body body = Instantiate(m_Rigidbody, spawnPos, new Quaternion(Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180)), m_Parent);
|
||||
body.m_Pelvis.gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(1, 1, 1));
|
||||
m_PelvisPosition = body.m_Pelvis.position;
|
||||
body.SetPool(m_Pool);
|
||||
return body;
|
||||
}
|
||||
|
||||
private void OnTakeBodyFromPool(Body body)
|
||||
{
|
||||
body.m_Pelvis.transform.position = m_PelvisPosition;
|
||||
body.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void OnReturnBodyToPool(Body body)
|
||||
{
|
||||
body.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnDestroyBody(Body body)
|
||||
{
|
||||
Destroy(body.gameObject);
|
||||
}
|
||||
|
||||
IEnumerator Spawn()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(Random.Range(3f, 7f));
|
||||
m_Pool.Get();
|
||||
StartCoroutine(Spawn());
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/8_Scripts/1_Managers/BodyManager.cs.meta
generated
Normal file
11
Assets/8_Scripts/1_Managers/BodyManager.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb69c2a4007c792419505438a2761ba7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
Assets/8_Scripts/1_Managers/NarrationManager.cs
Normal file
104
Assets/8_Scripts/1_Managers/NarrationManager.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
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);
|
||||
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(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()
|
||||
{
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/8_Scripts/1_Managers/NarrationManager.cs.meta
generated
Normal file
11
Assets/8_Scripts/1_Managers/NarrationManager.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98836c5e4d194524a9f515b29e1d7db1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
111
Assets/8_Scripts/1_Managers/SubtitleManager.cs
Normal file
111
Assets/8_Scripts/1_Managers/SubtitleManager.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using Mono.Cecil;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using static UnityEngine.ParticleSystem;
|
||||
|
||||
public class SubtitleManager : MonoBehaviour
|
||||
{
|
||||
[Header("Text")]
|
||||
public TMP_Text m_Text;
|
||||
[Header("Audio")]
|
||||
|
||||
private float m_TimeElapsed;
|
||||
|
||||
private bool m_FirstRun;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
NarrationManager.m_UpdateSubtitile += UpdateText;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
NarrationManager.m_UpdateSubtitile -= UpdateText;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
m_TimeElapsed = 0;
|
||||
m_Text.text = "";
|
||||
m_FirstRun = true;
|
||||
}
|
||||
|
||||
private void UpdateText(string sentence, float waitTime)
|
||||
{
|
||||
//FMOD.Studio.PLAYBACK_STATE state;
|
||||
|
||||
//m_NarrationSFXInst = AudioManager.instance.PlaySound(m_NarrationSFX);
|
||||
|
||||
//m_NarrationSFXInst.getPlaybackState(out state);
|
||||
|
||||
//if (m_FirstRun)
|
||||
//{
|
||||
// m_FirstRun = false;
|
||||
// m_NarrationSFXInst.start();
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// m_NarrationSFXInst.getPlaybackState(out state);
|
||||
// if (state == PLAYBACK_STATE.STOPPED)
|
||||
// {
|
||||
// m_NarrationSFXInst.start();
|
||||
// }
|
||||
//}
|
||||
StartCoroutine(TextScroll(sentence, waitTime));
|
||||
}
|
||||
|
||||
// Wait time, how long the text will stay on the screen for + if the audio stops late, apply an offset
|
||||
private IEnumerator ClearText(float waitTime, bool earlyStop)
|
||||
{
|
||||
var offset = (waitTime - m_TimeElapsed) * Random.Range(0.6f, 0.95f);
|
||||
|
||||
if(offset < 3)
|
||||
{
|
||||
offset = Random.Range(5f, 7f);
|
||||
}
|
||||
|
||||
Debug.Log($"offset{offset}, wt{waitTime}");
|
||||
float earlyOffset = 0;
|
||||
|
||||
if (!earlyStop)
|
||||
{
|
||||
earlyOffset = offset / Random.Range(2, 4);
|
||||
offset -= earlyOffset;
|
||||
yield return new WaitForSecondsRealtime(earlyOffset);
|
||||
//AudioManager.instance.StopSound(m_NarrationSFXInst);
|
||||
}
|
||||
yield return new WaitForSecondsRealtime(offset);
|
||||
m_Text.text = "";
|
||||
m_TimeElapsed = 0;
|
||||
}
|
||||
|
||||
private IEnumerator TextScroll(string sentence, float waitTime)
|
||||
{
|
||||
var characterLength = sentence.ToCharArray().Length;
|
||||
int randomStoppingValue = (int)Random.Range(characterLength * 0.6f, characterLength);
|
||||
var index = 0;
|
||||
bool earlyStop = false;
|
||||
|
||||
if (Random.Range(0f, 10f) > 6) earlyStop = true;
|
||||
|
||||
foreach (char c in sentence.ToCharArray())
|
||||
{
|
||||
index++;
|
||||
m_Text.text += c;
|
||||
float randomInterval = Random.Range(0.001f, 0.002f);
|
||||
yield return new WaitForSecondsRealtime(randomInterval);
|
||||
m_TimeElapsed += randomInterval;
|
||||
|
||||
// Stop audio early based on probability
|
||||
if (randomStoppingValue == index && earlyStop)
|
||||
{
|
||||
//AudioManager.instance.StopSound(m_NarrationSFXInst);
|
||||
}
|
||||
}
|
||||
|
||||
StartCoroutine(ClearText(waitTime, false));
|
||||
}
|
||||
}
|
||||
11
Assets/8_Scripts/1_Managers/SubtitleManager.cs.meta
generated
Normal file
11
Assets/8_Scripts/1_Managers/SubtitleManager.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a056a38641a8da54e8ba4d7c54f2a1cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user