简体   繁体   English

Unity中更新方法的延迟

[英]delay in update method in Unity

I am making a badminton simulator in unity, where the opponent is a set of video clips.我正在统一制作一个羽毛球模拟器,其中对手是一组视频剪辑。 I am trying to add some delay to my update method so theres some time between two clips of the opponent.我正在尝试为我的更新方法添加一些延迟,以便在对手的两个剪辑之间有一些时间。 However this delay only applies to the video clips and not the shuttle that arrives from behind the video.但是,此延迟仅适用于视频剪辑,而不适用于从视频后面到达的穿梭机。 My Code:我的代码:

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;


    VideoClip nextClip;
    private bool Timer;
    void Start()
    {
        Shuffle(playlist);
        // play the first video in the playlist
        PrepareNextPlaylistClip();
        SwitchCams(activeCam);
        Timer=false; 
        // setup an event to automatically call SwitchCams() when we finish playing
        activeCam.loopPointReached += SwitchCams;
        otherCam.loopPointReached += SwitchCams;
        shuttle.SetActive(false);
    
    }

    void Update()
    {
        if (playlist.Count == 0)
                return;
        if(!Timer)
        {
            
            StartCoroutine(CountDown(5));
            
            if (nextClip == null && activeCam.time >= activeCam.clip.length - 0.1)
            {
                PrepareNextPlaylistClip();
                shuttle.SetActive(false);
            }
        
            if(activeCam.time >= 1.0f && activeCam.time <= 2.95f)
            {
                Debug.Log("start:"+activeCam.time);
                shuttle.SetActive(true);
            }
            else
            //if(activeCam.time >= 2.95f || activeCam.time <= 1.0f)
            {
                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 PrepareNextPlaylistClip()
    {

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


    //delay couroutine
    IEnumerator CountDown(float delay)
    {
        Timer = true;
        yield return new WaitForSeconds(delay);
        Timer= false;
    }
    // randomize the video playlist
    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;
        }
    }
}

Forgive me if I'm misunderstanding your code but rather than having it all in Update() couldn't you just have it in an IEnumerator like this?原谅我,如果我误解了你的代码,而不是把它全部放在Update() ,你不能把它放在像这样的IEnumerator吗?

void Start()
{ 
    Shuffle(playlist);
    // play the first video in the playlist
    PrepareNextPlaylistClip();
    SwitchCams(activeCam);        
    activeCam.loopPointReached += SwitchCams;
    otherCam.loopPointReached += SwitchCams;
    shuttle.SetActive(false);

    //Run the function on start
    StartCoroutine(Function());
}
IEnumerator Function()
    {
        while(true)
        {
            if(playlist.Count == 0)
            {
                //If you have no clips left exit out of the loop
                break;
            }
            if(nextClip == null)
            {
                //If you have clips left load the next clip
                shuttle.SetActive(false);
                PrepareNextPlaylistClip();
            }
            
            yield return new WaitForSeconds(1); //This is your delay
            //Execute the code you want to run after the delay here
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM