如何在Unity3d Video Player中更改视频片段?

问题描述 投票:0回答:1

我真的是Unity程序的新手。我正在Univ上VR内容课程。

我正在尝试在一个Target RenderTexture上播放多个视频。当我单击上面带有RenderTexture的GameObeject时,我想更改视频。所以我做了一些代码并将其应用于Plane GameObject

但是它说“” Plane(3)“游戏对象没有附加'VideoPlayer',但是脚本正在尝试访问它。您可能需要将VideoPlayer添加到游戏对象“飞机(3)”。或者您的脚本需要在使用组件之前检查组件是否已连接。“

我该如何解决?请帮助我。.

代码

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

public class Videomanager : MonoBehaviour
{
    public VideoClip[] videoClips;
    private VideoPlayer videoPlayer;
    private int videoClipIndex;
    public RenderTexture Target;

    void Awake()
    {
        videoPlayer = GetComponent<VideoPlayer> ();
    }

    void Start()
    {
        videoPlayer.targetTexture = Target;
        videoPlayer.clip = videoClips[4];
    }

    public void SetNextClip()
    {
        videoClipIndex++;
        if(videoClipIndex >= videoClips.Length)
        {
            videoClipIndex = videoClipIndex % videoClips.Length;
        }

        videoPlayer.clip = videoClips[videoClipIndex];
        videoPlayer.Play();
    }

    // Start is called before the first frame update
    void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            SetNextClip();
        }
    }


    // Update is called once per frame
    void Update()
    {

    }
}
unity3d video-player
1个回答
0
投票

您必须在videoplayer.play()之前调用videoplayer.prepare()。

prepare()将需要一些时间来准备视频。

代码

[SerializeField] protected RawImage _videoBackground; // it holds reference to the raw image of videoplayer gameobject. 

public virtual void PlayGoalVideo(int index) // Play the video from an Index.
{
    Debug.Log ("Index is : " + index + " File name: " + _goalVideoFileNames [index]);

    if (!string.IsNullOrEmpty (_goalVideoFileNames [index])) {
        _videoPlayer.url = _goalVideoFileNames [index];
        _videoPlayer.Prepare ();
        StartCoroutine (playVideo ());
    }
}


protected virtual IEnumerator playVideo() // using a coroutine to wait for video to get prepared.
{
    Debug.Log ("Starting to prepare video");
    yield return new WaitUntil (() => _videoPlayer.isPrepared);
    Debug.Log ("Video has been prepared");

    _videoPlayer.Play ();
    Debug.Log ("Waiting for video to start playing!");
    yield return new WaitUntil (() => _videoPlayer.isPlaying);
    VideoBackground.enabled = true; 
    Debug.Log ("Background has been enabled!");

    _videoBackground.texture = _videoPlayer.texture;
    _videoBackground.color = _playColor; // play color will set the alpha to make it visible[this is to avoid any black screen while the videoplayer is preparing the video]
}

public virtual void StopVideo() // use this to stop the video.
{
    if (!IsPlaying)
    {
        return;
    }
    _videoPlayer.Stop();

    _videoBackground.color = _defaultColor;
}

PS:如果需要帮助,请告诉我:)

© www.soinside.com 2019 - 2024. All rights reserved.