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.Log("2322");//用于调试脚本不能正常运行
 
           }
          else if (!videoplayer.isPlaying)
          {
            videoplayer.Play();
            Debug.Log("111");
            text_playorpause.text = "暂停";
          }
        }
    }
    private void OnpreVideo() {
        currentClipIndex -= 1;
        if (currentClipIndex < 0) {
            currentClipIndex = videoClips.Length - 1;
        }
        videoplayer.clip = videoClips[currentClipIndex];
        text_playorpause.text = "暂停";
        }
    private void OnnextVideo()
    {
        currentClipIndex += 1;
        currentClipIndex = currentClipIndex % videoClips.Length;
        videoplayer.clip = videoClips[currentClipIndex];
        text_playorpause.text = "暂停";
    }
    }
    
 

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

1、在Hierarchy中鼠标右击,选择Ultraleap中的Interaction下的3D Button,共创建了三个,分别为UpStopDownOrder
2、修改3D Button下的脚本控件Simple Interaction Glow,代码见5
3、将所要播放的视频,放在Video Clips下
4、将脚本Simple Interaction Glow移动到OnPress()和On Unpress()上,并选择所要执行的函数接口
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
5、Simple Interaction Glow控件代码如下

/******************************************************************************
 * Copyright (C) Ultraleap, Inc. 2011-2024.                                   *
 *                                                                            *
 * Use subject to the terms of the Apache License 2.0 available at            *
 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement           *
 * between Ultraleap and you, your company or other organization.             *
 ******************************************************************************/

using Leap.Unity;
using Leap.Unity.Interaction;
using UnityEngine;
using System.Diagnostics; // 引入System.Diagnostics命名空间

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

namespace Leap.Unity.InteractionEngine.Examples
{
    /// <summary>
    /// This simple script changes the color of an InteractionBehaviour as
    /// a function of its distance to the palm of the closest hand that is
    /// hovering nearby.
    /// </summary>
    [AddComponentMenu("")]
    [RequireComponent(typeof(InteractionBehaviour))]
    public class SimpleInteractionGlow : MonoBehaviour
    {
        private VideoPlayer videoplayer;
        private RawImage rawImage;
        private int currentClipIndex;

        public VideoClip[] videoClips;//定义了一个数组
        
        [Tooltip("If enabled, the object will lerp to its hoverColor when a hand is nearby.")]
        public bool useHover = true;

        [Tooltip("If enabled, the object will use its primaryHoverColor when the primary hover of an InteractionHand.")]
        public bool usePrimaryHover = false;

        [Header("InteractionBehaviour Colors")]
        public Color defaultColor = Color.Lerp(Color.black, Color.white, 0.1F);

        public Color suspendedColor = Color.red;
        public Color hoverColor = Color.Lerp(Color.black, Color.white, 0.7F);
        public Color primaryHoverColor = Color.Lerp(Color.black, Color.white, 0.8F);

        [Header("InteractionButton Colors")]
        [Tooltip("This color only applies if the object is an InteractionButton or InteractionSlider.")]
        public Color pressedColor = Color.white;

        private Material[] _materials;

        private InteractionBehaviour _intObj;

        [SerializeField]
        private Rend[] rends;

        [System.Serializable]
        public class Rend
        {
            public int materialID = 0;
            public Renderer renderer;
        }

        public void Press()
        {
            UnityEngine.Debug.Log("按下");
          if (videoplayer.isPlaying)
            { 
                UnityEngine.Debug.Log("Pause");
                videoplayer.Pause();
            }
          else if (!videoplayer.isPlaying)
            {
                UnityEngine.Debug.Log("Play");
                videoplayer.Play();
            }
        }

        public void Unpress()
        {
           UnityEngine.Debug.Log("抬起");
        }

        public void OnpreVideo() {
            currentClipIndex -= 1;
            if (currentClipIndex < 0) {
                currentClipIndex = videoClips.Length - 1;
            }
            videoplayer.clip = videoClips[currentClipIndex];
    
        }

        public void OnnextVideo()
        {
            currentClipIndex += 1;
            currentClipIndex = currentClipIndex % videoClips.Length;
            videoplayer.clip = videoClips[currentClipIndex];
            
        }
         // 打开指定程序,根据实际情况自行修改
        public void OpenExe()
        {
            string filePath = @"C:\Program Files\Ultraleap\TouchFree\TouchFree\TouchFree.exe"; // 要打开的程序路径
            // 对文件路径中的反斜杠进行转义
            string escapedPath = filePath.Replace("\\", "\\\\");
            Process.Start(escapedPath);

            string ApplicationfilePath = @"F:\ultraleap-touchfree-bakery-1-0-0-cursorless\Ultraleap TouchFreeBakery 1-0-0 - Cursorless\TouchFreeBakery.exe";
            string ApplicationPath = ApplicationfilePath.Replace("\\", "\\\\");
            Process.Start(ApplicationPath);
        }

        void Start()
        {
            _intObj = GetComponent<InteractionBehaviour>();
            UnityEngine.Debug.Log("_intObj的值是:" + _intObj);

            videoplayer = FindObjectOfType<VideoPlayer>();//获取组件
            UnityEngine.Debug.Log("videoplayer的值是:" + videoplayer);

            rawImage = FindObjectOfType<RawImage>();
            UnityEngine.Debug.Log("rawImage的值是:" + rawImage);

            currentClipIndex = 0;

            if (rends.Length > 0)
            {
                _materials = new Material[rends.Length];

                for (int i = 0; i < rends.Length; i++)
                {
                    _materials[i] = rends[i].renderer.materials[rends[i].materialID];
                }
            }
        }

        void Update()
        {
            if (_materials != null)
            {

                // The target color for the Interaction object will be determined by various simple state checks.
                Color targetColor = defaultColor;

                // "Primary hover" is a special kind of hover state that an InteractionBehaviour can
                // only have if an InteractionHand's thumb, index, or middle finger is closer to it
                // than any other interaction object.
                if (_intObj.isPrimaryHovered && usePrimaryHover)
                {
                    targetColor = primaryHoverColor;
                }
                else
                {
                    // Of course, any number of objects can be hovered by any number of InteractionHands.
                    // InteractionBehaviour provides an API for accessing various interaction-related
                    // state information such as the closest hand that is hovering nearby, if the object
                    // is hovered at all.
                    if (_intObj.isHovered && useHover)
                    {
                        float glow = _intObj.closestHoveringControllerDistance.Map(0F, 0.2F, 1F, 0.0F);
                        targetColor = Color.Lerp(defaultColor, hoverColor, glow);
                    }
                }

                if (_intObj.isSuspended)
                {
                    // If the object is held by only one hand and that holding hand stops tracking, the
                    // object is "suspended." InteractionBehaviour provides suspension callbacks if you'd
                    // like the object to, for example, disappear, when the object is suspended.
                    // Alternatively you can check "isSuspended" at any time.
                    targetColor = suspendedColor;
                }

                // We can also check the depressed-or-not-depressed state of InteractionButton objects
                // and assign them a unique color in that case.
                if (_intObj is InteractionButton && (_intObj as InteractionButton).isPressed)
                {
                    targetColor = pressedColor;
                }

                // Lerp actual material color to the target color.
                for (int i = 0; i < _materials.Length; i++)
                {
                    _materials[i].color = Color.Lerp(_materials[i].color, targetColor, 30F * Time.deltaTime);
                }
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值