A bunch of stuff
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(TextMeshProUGUI))]
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class VoiceLinePlayer : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public class CloseCaption
|
||||
{
|
||||
public string text;
|
||||
public float timing;
|
||||
}
|
||||
|
||||
// singleton
|
||||
public static VoiceLinePlayer Instance { get; private set; }
|
||||
|
||||
|
||||
// references
|
||||
private TextMeshProUGUI _subtitleText;
|
||||
private AudioSource _audioSource;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
SetInstance();
|
||||
_subtitleText = GetComponent<TextMeshProUGUI>();
|
||||
_audioSource = GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
SetInstance();
|
||||
}
|
||||
|
||||
private void SetInstance()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("VoiceLinePlayer Instance already exists.");
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_audioSource.isPlaying)
|
||||
{
|
||||
_subtitleText.text = "";
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayLine(AudioClip clip, List<CloseCaption> cc)
|
||||
{
|
||||
_audioSource.clip = clip;
|
||||
_audioSource.Play();
|
||||
foreach (var caption in cc)
|
||||
{
|
||||
StartCoroutine(ShowCaption(caption.text, caption.timing));
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator ShowCaption(string text, float timing)
|
||||
{
|
||||
yield return new WaitForSeconds(timing);
|
||||
_subtitleText.text = text;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user