VR游乐园学习(1)—添加游乐项目视频

感情迁移 提交于 2019-11-29 00:11:13

(1)首先需要添加组件“VideoPlayer”

source为视频来源,URL为视频路径。

(2)通过脚本控制视频播放,在Quad上挂载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
using System.IO;

public class GameItem : MonoBehaviour {

    public int Index;
    private VideoPlayer m_VideoPlayer;

    private void Awake()
    {
        m_VideoPlayer = GetComponent<VideoPlayer>();
        GameObject.Find("ControllerRight").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_TouchpadReleased;
        GameObject.Find("ControllerLeft").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItem_TouchpadReleased;
    }
    private void Update()
    {
        if (Index == GameItemSelect._Instance.Index)
        {
            GetComponent<MeshCollider>().enabled = true;
            GetComponent<MeshRenderer>().material.color = Color.white;
        }else
        {
            GetComponent<MeshCollider>().enabled = false;
            GetComponent<MeshRenderer>().material.color = Color.gray;
        }
    }
    /// <summary>
    /// 圆盘按钮抬起
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void GameItem_TouchpadReleased(object sender, ControllerInteractionEventArgs e)
    {
        m_VideoPlayer.Pause();
    }
    /// <summary>
    /// 设置视频名称
    /// </summary>
    /// <param name="videoName"></param>
    public void SetVideoName(string videoName)
    {
        m_VideoPlayer.url = GetVideoPath(videoName);

    }
    /// <summary>
    /// 获取视频路径
    /// </summary>
    /// <param name="videoName"></param>
    /// <returns></returns>
    private string GetVideoPath(string videoName)
    {
        return Application.dataPath + "/StreamingAssets/" + videoName + ".mp4";

    }
    private void OnTriggerEnter(Collider other)
    {
        if (File.Exists(m_VideoPlayer.url) == false) return;
        m_VideoPlayer.Play();
    }

    private void OnTriggerExit(Collider other)
    {
        m_VideoPlayer.Pause();
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!