在Unity上透過UI來操作影片的播放還算簡單,在這個例子中,我們需要VideoPlayer、Render Texture、Button以及RawImage,以及幾個要播放的影片,首先,設定的物件階層如下所示:
直接來看程式碼的部份:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class myvplayer : MonoBehaviour
{
public VideoPlayer vp;
public VideoClip[] clips;
private int index = 0;
void Start()
{
vp.clip = clips[index];
}
public void NextVideo()
{
if (++index>2) index = 0;
vp.clip = clips[index];
vp.Stop();
vp.Play();
}
public void PrevVideo()
{
if(--index<0) index = 2;
vp.clip = clips[index];
vp.Stop();
vp.Play();
}
public void PlayVideo()
{
vp.Play();
}
public void StopVideo()
{
vp.Stop();
}
public void PauseVideo()
{
vp.Pause();
}
}