You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity羽毛球模拟器开发:Update方法添加延迟仅对视频生效问题

问题根因
  • 协程启动逻辑错误:你在Update方法中每帧判断Timer == false就启动一次延迟协程,相当于每秒会触发几十次协程调用,Timer状态被反复重置,延迟逻辑完全不符合预期
  • 视频和羽毛球逻辑解耦:下一段视频的播放调用在PrepareNextPlaylistClip中直接执行,和你写的延迟逻辑完全无关,视频能生效只是巧合;而羽毛球的显示逻辑和activeCam的播放时间绑定,activeCam的时间不会因为你的延迟协程停止,所以羽毛球逻辑不受延迟影响
修正方案

你需要把延迟逻辑绑定到视频播放结束的回调上,延迟期间暂停下一段视频,延迟结束后再切换播放,同时重置羽毛球状态,修正后的完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class Video_Player : MonoBehaviour
{
    public VideoPlayer activeCam, otherCam;
    public List<VideoClip> playlist = new List<VideoClip>();
    public GameObject shuttle;
    public float intervalBetweenClips = 5f; // 两段视频的间隔时间,可在Inspector调整

    VideoClip nextClip;
    private bool isInInterval = false;

    void Start()
    {
        Shuffle(playlist);
        PrepareNextPlaylistClip();
        SwitchCams(activeCam);
        shuttle.SetActive(false);
        // 绑定播放结束回调
        activeCam.loopPointReached += OnVideoEnd;
        otherCam.loopPointReached += OnVideoEnd;
    }

    void Update()
    {
        if (playlist.Count == 0 || isInInterval)
            return;

        if (nextClip == null && activeCam.time >= activeCam.clip.length - 0.1f)
        {
            PrepareNextPlaylistClip();
            shuttle.SetActive(false);
        }

        if(activeCam.time >= 1.0f && activeCam.time <= 2.95f)
        {
            Debug.Log("start:"+activeCam.time);
            shuttle.SetActive(true);
        }
        else
        {
            Debug.Log("end:"+activeCam.time);
            shuttle.SetActive(false);
        }
    }

    void SwitchCams(VideoPlayer thisCam)
    {
        activeCam = otherCam;
        otherCam = thisCam;
        activeCam.targetCameraAlpha = 1f;
        otherCam.targetCameraAlpha = 0f;
        
        Debug.Log("new clip: " + nextClip.name);
        nextClip = null;
    }

    // 视频播放结束触发,先进入间隔延迟
    void OnVideoEnd(VideoPlayer thisCam)
    {
        StartCoroutine(WaitForNextClip(thisCam));
    }

    IEnumerator WaitForNextClip(VideoPlayer endedCam)
    {
        isInInterval = true;
        shuttle.SetActive(false);
        // 暂停预加载好的下一段视频,等待延迟结束
        otherCam.Pause();
        yield return new WaitForSeconds(intervalBetweenClips);
        // 延迟结束后切换摄像头播放
        SwitchCams(endedCam);
        activeCam.Play();
        isInInterval = false;
    }

    void PrepareNextPlaylistClip()
    {
        nextClip = playlist[0];
        otherCam.clip = nextClip;
        otherCam.Play();
        playlist.RemoveAt(0);
    }

    // 随机打乱播放列表
    public static void Shuffle<T>(IList<T> playlist)
    {
        int n = playlist.Count;
        while (n > 1)
        {
            n--;
            int k = Random.Range(0, n);
            T value = playlist[k];
            playlist[k] = playlist[n];
            playlist[n] = value;
        }
    }
}
核心修改说明
  • 移除了Update中的协程启动逻辑,将延迟绑定到视频播放结束的回调事件上,避免重复触发协程
  • 新增了间隔状态标记isInInterval,延迟期间会直接跳过Update中的羽毛球逻辑判断,保证间隔时间内羽毛球不会被错误显示
  • 延迟期间会暂停预加载的下一段视频,避免视频时间提前走,保证视频和羽毛球逻辑的时间对齐

内容的提问来源于stack exchange,提问作者Sanket Kokane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 02:24:06