Brain_Arduino/Assets/Scripts/AudioPlayControl.cs

44 lines
1.1 KiB
C#

using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Video;
[RequireComponent(typeof(VideoPlayer))]
public class AudioPlayControl : MonoBehaviour
{
public VideoClip startAudio;
public VideoClip endAudio;
private VideoPlayer player;
private void Awake()
{
player = GetComponent<VideoPlayer>();
player.playOnAwake = false;
}
public async Task PlayStartVideo()
{
this.gameObject.SetActive(true);
player.clip = startAudio;
player.Play();
await Task.Delay((int)(1000 * player.clip.length));
if (this == null || this.gameObject == null)
return;
player.Stop();
this.gameObject.SetActive(false);
}
public async void PlayEndVideo()
{
player.clip = endAudio;
player.Play();
await Task.Delay((int)(1000 * player.clip.length));
player.Stop();
this.gameObject.SetActive(false);
}
public void StopPlay()
{
player.Stop();
this.gameObject.SetActive(false);
}
}