如何在第一个摄像头仍处于打开状态时打开第二个摄像头几秒钟(从而模拟空白屏幕)



我正在场景中运行一系列视频。我想在视频之间模拟 2 秒的空白屏幕。我正在使用两个相机,主相机和第二相机。第二个相机具有纯色,黑色背景和剔除蒙版设置为"无"。第一个视频播放结束后,我可以从日志中找到第二个摄像头已启用 - 但效果没有实现,因为我在第二个视频开始后立即禁用了它。如何让第二个摄像头保持打开一段时间 - 比如 2 秒?

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));
}

没有错误消息 - 但我无法在一段时间内保持第二台相机打开。

创建一个计时器变量:

float second_camera_timer = 0;

Update()递减它:

second_camera_timer -= Time.deltaTime ();

启用第二个摄像头时,将计时器设置为 2 秒:

second_camera_timer = 2f;

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

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

最新更新