当第一台相机仍在开机时,如何打开第二台相机几秒钟(从而模拟黑屏) [英] How can I switch on the second camera for few sec (thereby simulating a blank screen) while the first camera is still on

查看:101
本文介绍了当第一台相机仍在开机时,如何打开第二台相机几秒钟(从而模拟黑屏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在场景中播放一系列视频.我想在两个视频之间模拟2秒的黑屏.我正在使用两个摄像头,分别是Main和Second.第二台摄像机具有纯色,黑色背景,并且消光蒙版设置为无".在第一个视频播放结束后,我可以从日志中找到启用了第二个摄像头的效果-但是,由于我在第二个视频开始播放后立即禁用了第二个摄像头,因此无法实现效果.我如何保持第二台相机打开一段时间(例如2秒钟)?

I am running a sequence of videos inside a scene. I want to simulate a blank screen of 2 seconds in between videos. I am using two camera's, Main and Second. The Second Camera has Solid Color, Black background and Culling Mask is set to "Nothing". After the first video playback is over, I can find from logs that second camera is enabled - but the effect is not realised since I am disabling it immediately since my second video starts. How can I keep the second camera on for some time - say 2 seconds?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.Audio;
using UnityEngine.UI;
using UnityEngine.Rendering;


public class Play3DVideoSequenceAmbisonics : MonoBehaviour
{
    public List<VideoClip> videoClipList; //list of video clips
    public List<AudioClip> audioClipList; //list of audio clips
    public List<GameObject> audioSources;

    private List<VideoPlayer> videoPlayerList;
    private int videoIndex = 0; //videoIndex is 0 initially

    [SerializeField]
    private RenderTexture _renderTexture = null;

    VideoPlayer videoPlayer;
    AudioSource audioData;


    public Camera m_MainCamera;
    public Camera m_CameraTwo;

    // Start is called before the first frame update
    void Start()
    {

        m_MainCamera = Camera.main;
        m_MainCamera.enabled = true;
        m_CameraTwo.enabled = false;

        StartCoroutine(PlayVideo());
    }


    // Update is called once per frame
    IEnumerator PlayVideo(bool firstRun = true)
    {
        if (videoClipList == null || videoClipList.Count <= 0)
        {
            Debug.LogError("Assign VideoClips from the Editor");
            yield break;
        }

        //Init videoPlayerList first time this function is called
        if (firstRun)
        {
            videoPlayerList = new List<VideoPlayer>();

            for (int i = 0; i < videoClipList.Count; i++)
            {

                //Create new Object to hold the Video and the sound then make it a child of this object
                GameObject vidHolder = new GameObject("VP" + i);
                audioSources.Add(GameObject.Find("VP" + i));

                vidHolder.transform.SetParent(transform);

                //Add VideoPlayer to the GameObject
                videoPlayer = vidHolder.AddComponent<VideoPlayer>();
                videoPlayerList.Add(videoPlayer);

                //Add AudioSource to  the GameObject
                audioData = vidHolder.AddComponent<AudioSource>();

                //Enable Play on Awake for both Video and Audio
                videoPlayer.playOnAwake = false;
                audioData.playOnAwake = false;

                videoPlayer.renderMode = VideoRenderMode.RenderTexture;

                //We want to play from video clip not from url
                videoPlayer.source = VideoSource.VideoClip;

                //Set Audio Output to AudioSource
                videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

                //Assign the Audio from Video to AudioSource to be played
                //videoPlayer.EnableAudioTrack(1, true);
                //videoPlayer.SetTargetAudioSource(0, audioSource);

                //Set video Clip To Play 
                videoPlayer.clip = videoClipList[i];
                audioData.clip = audioClipList[i];

                videoPlayer.targetTexture = _renderTexture;
            }
        }

        //Make sure that the NEXT VideoPlayer index is valid
        if (videoIndex >= videoPlayerList.Count)
            yield break;


        //Prepare video
        videoPlayerList[videoIndex].Prepare();

        //Wait until this video is prepared
        while (!videoPlayerList[videoIndex].isPrepared)
        {
            Debug.Log("Preparing Index: " + videoIndex);
            yield return null;
        }
        Debug.Log("Done Preparing current Video Index: " + videoIndex);

        m_CameraTwo.enabled = false;
        Debug.Log("The current video index is:" + videoIndex);
        Debug.Log("The main camera is:" + m_MainCamera.enabled);
        Debug.Log("The second camera is:" + m_CameraTwo.enabled);

        //Play first video
        videoPlayerList[videoIndex].Play();

        audioSources[videoIndex].GetComponent<AudioSource>().Play();
        audioSources[videoIndex].GetComponent<AudioSource>().spatialBlend = 1;
        audioSources[videoIndex].GetComponent<AudioSource>().spread = 360;


        //Wait while the current video is playing
        bool reachedHalfWay = false;
        int nextIndex = (videoIndex + 1);


        while (videoPlayerList[videoIndex].isPlaying)
        {
            Debug.Log("Playing time: " + videoPlayerList[videoIndex].time + " INDEX: " + videoIndex);

            //(Check if we have reached half way)
            if (!reachedHalfWay && videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length / 2))
            {
                reachedHalfWay = true; //Set to true so that we don't evaluate this again

                //Make sure that the NEXT VideoPlayer index is valid. Othereise Exit since this is the end
                if (nextIndex >= videoPlayerList.Count)
                {
                    Debug.Log("End of All Videos: " + videoIndex);
                    yield break;
                }

                //Prepare the NEXT video
                Debug.Log("Ready to Prepare NEXT Video Index: " + nextIndex);
                videoPlayerList[nextIndex].Prepare();
            }
            yield return null;
        }
        Debug.Log("Done Playing current Video Index: " + videoIndex);


        //Wait until NEXT video is prepared
        while (!videoPlayerList[nextIndex].isPrepared)
        {
            Debug.Log("Preparing NEXT Video Index: " + nextIndex);
            yield return null;
        }

        Debug.Log("Done Preparing NEXT Video Index: " + videoIndex);

        m_CameraTwo.enabled = true;
        Debug.Log("The current video index is:" + videoIndex);
        Debug.Log("The main camera is:" + m_MainCamera.enabled);
        Debug.Log("The second camera is:" + m_CameraTwo.enabled);


        //Increment Video index
        videoIndex++;

         //Play next prepared video. Pass false to it so that some codes are not executed at-all
        StartCoroutine(PlayVideo(false));

    }

没有错误消息-但我无法将第二台相机打开一段时间.

There are no error messages - but I am unable to keep the second camera on for some time.

推荐答案

创建计时器变量:

float second_camera_timer = 0;

每个Update()您将其递减:

second_camera_timer -= Time.deltaTime ();

当您启用第二台摄像机时,将计时器设置为2秒:

And when you enable your 2nd camera, set the timer to 2 seconds:

second_camera_timer = 2f;

但是在禁用它之前,请检查计时器是否为0:

But before disabling it, check if timer is 0:

if (second_camera_timer <= 0f)
{
    m_CameraTwo.enabled = false;
}

这篇关于当第一台相机仍在开机时,如何打开第二台相机几秒钟(从而模拟黑屏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆