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("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
,共创建了三个,分别为Up
、Stop
、Down
、Order
。
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);
}
}
}
}
}