0、本节项目下载链接
1、在Unity中添加视频显示界面
1、在Hierarchy
中鼠标右击,选择UI
中的Raw Image
2、将所要显示的视频放在Scences
下,并单击视频文件,在Inspector
中,选择Transcode
,并将Dimension
设置为Square 1024
,将Aspect Ratio
设置为Stretch
,然后单击右下角的Apply
。
3、在RawImage
的Inspector
中添加Video Player
组件
②设置Source
为Video Clip
③将设置完的视频添加到RawImage
中Video Player
下的Video Clip
中。
②设置Render Mode
为Render Texture
③右击Asset
,选择create
下的Render Texture
,创建一个新的Render Texture
,并命名为video
,将其添加到Video Player
下的Target TexTure
和Raw Image
下的Texture
4、运行即可
2、Unity默认Button,控制多个视频切换
1、在RawImage
下添加Scripts
控件Video_Controller
,代码见5
2、在RawImage创建三个子物体Button
(Unity默认Button),分别为Up
、Stop
、Down
,参数默认
3、将三个子物体Button
,添加到RawImage
的脚本变量端口处
4、运行即可
5、脚本控件`Video_Controller’代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class Video_Controller : MonoBehaviour {
private VideoPlayer videoplayer;
private RawImage rawImage;
private int currentClipIndex;
public Text text_playorpause;
public Button button_playorpause;
public Button button_pre;
public Button button_next;
public VideoClip[] videoClips;//定义了一个数组
// Use this for initialization
void Start () {
videoplayer = this.GetComponent<VideoPlayer>();//获取组件
rawImage = this.GetComponent<RawImage>();
currentClipIndex = 0;
button_playorpause.onClick.AddListener(OnplayorpauseVideo);
button_pre.onClick.AddListener(OnpreVideo);
button_next.onClick.AddListener(OnnextVideo);//调用方法
}
// Update is called once per frame
void Update () {
if (videoplayer.texture == null)
{
return;
}
rawImage.texture = videoplayer.texture;
}
private void OnplayorpauseVideo() {
if (videoplayer.enabled == true)
{
if (videoplayer.isPlaying) {
videoplayer.Pause();
text_playorpause.text = "播放";
Debug