我对使用Unity非常陌生,并且可以从YouTube的教程中幸存下来.我的游戏启动后,便会使用视频播放器开始播放视频.我希望在视频播放完毕后将其隐藏起来,以显示我的菜单屏幕.我确实有一个用来隐藏视频播放器的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehavIoUr
{
public GameObject VideoPlayer;
public void HideVideoPlayer()
{
VideoPlayer.gameObject.SetActive(false);
}
}
问题是,我实际上最接近隐藏视频的方法是通过按钮将其设置为onclick事件.视频播放完毕后,如何隐藏视频播放器?谢谢.
解决方法:
当它停止播放并将其放入Update时,为什么不直接隐藏它呢?
void Update() {
if (!(VideoPlayer.isPlaying)) {
VideoPlayer.gameObject.SetActive(false);
}
}
完整的脚本如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehavIoUr
{
public GameObject VideoPlayer;
public bool isPlayerStarted = false;
void Update() {
if (isPlayerStarted == false && VideoPlayer.IsPlaying == true) {
// When the player is started, set this information
isPlayerStarted = true;
}
if (isPlayerStarted == true && VideoPlayer.isPlaying == false ) {
// Wehen the player stopped playing, hide it
VideoPlayer.gameObject.SetActive(false);
}
}
}