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.Log

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



