如何在时间到达后隐藏UI对象(视频播放器)(Unity)

3

我非常新手使用Unity,仅依靠YouTube上的教程生存。游戏启动后,立即使用Video Player开始播放视频。我希望在视频播放完毕后隐藏视频,以显示我的菜单屏幕。我有一个脚本,用于隐藏视频播放器。

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事件。如何使视频播放完后隐藏视频播放器?谢谢。

你尝试过使用 VideoPlayer.isPlaying 吗?如果你不需要循环播放视频,你可以使用这个或者你也可以使用 VideoPlayer.loopPointReached - Ali Kanat
1个回答

4
为什么不在停止播放时隐藏它,并将其放在“更新”中呢?
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);
        }
    }   
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接