LaboratoryProtection/Assets/UnityTest/ZXL/Scripts/UI/Speak.cs

40 lines
864 B
C#
Raw Normal View History

2023-10-05 02:31:29 +08:00
using System.Collections;
using TMPro;
using UnityEngine;
namespace UnityTest.ZXL
{
public class Speak : MonoBehaviour
{
public TextMeshProUGUI speakContent;
private AudioSource _audioSource;
private void Awake()
{
_audioSource = GetComponent<AudioSource>();
gameObject.SetActive(false);
}
private void OnEnable()
{
if (_audioSource.clip != null)
{
StartCoroutine(PlayAudio());
}
}
IEnumerator PlayAudio()
{
var length = _audioSource.clip.length;
yield return new WaitForSeconds(length);
_audioSource.clip = null;
}
private void OnDisable()
{
StopAllCoroutines();
_audioSource.clip = null;
}
}
}