Unity中使用Ultraleap中的3D Button实现视频切换

本文详细介绍了如何在Unity中使用Ultraleap的3DButton配合VideoPlayer组件,实现视频的播放、暂停以及切换功能,同时展示了如何通过脚本来控制多个视频的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0、本节项目下载链接

Ultraleap中的3D Button实现视频切换

1、在Unity中添加视频显示界面

1、在Hierarchy中鼠标右击,选择UI中的Raw Image

2、将所要显示的视频放在Scences下,并单击视频文件,在Inspector中,选择Transcode,并将Dimension设置为Square 1024将Aspect Ratio设置为Stretch,然后单击右下角的Apply
在这里插入图片描述
3、在RawImageInspector中添加Video Player组件
②设置SourceVideo Clip
③将设置完的视频添加到RawImageVideo Player下的Video Clip中。
②设置Render ModeRender Texture
③右击Asset,选择create下的Render Texture,创建一个新的Render Texture,并命名为video,将其添加到Video Player下的Target TexTureRaw Image下的Texture
在这里插入图片描述
4、运行即可

2、Unity默认Button,控制多个视频切换

1、在RawImage下添加Scripts控件Video_Controller,代码见5
2、在RawImage创建三个子物体Button(Unity默认Button),分别为UpStopDown,参数默认
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值