VR遊樂園學習(4)—添加視頻

一、在預製體上添加組件ide

 

二、添加組件「GameItem」控制視頻播放url

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 == GameItemSpawn.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();
    }
}
View Code
相關文章
相關標籤/搜索