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.
74 lines
1.6 KiB
74 lines
1.6 KiB
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;
|
|
}
|
|
}
|