一、VR环境配置
1.创建空物体,命名为【VRTK_SDK_MANAGER】,为其添加组件VRTK_SDK Manager,设置Setups,Scripts Aliases
2.在【VRTK_SDK_MANAGER】下方添加SDKSetUps预制体
3.在添加SDKSetupSwitcher预制体
4.创建空对象,命名为【VRTK_Scripts】,在其下创建2个空对象,分别命名为ControllerRight,ControllerLeft,并为其添加组件VRTK_Pointer,并设置其Pointer Renderer
VRTK_Straight Pointer Renderer,VRTK_Controller Events
可以控制左右手柄发射射线,射线打到碰撞体身上显示绿色,没打到显示红色。在VRTK_Poiner组件可以在Selection Button改变手柄按键。
二、UI环境搭建
如果想要在UI上射线能检测到,要在Canvas上添加VRTK_UI Canvas组件,同时ControllerRight,ControllerLeft对象上要添加VRTK_UI Pointer组件,并且UI对象要加Box Collider碰撞体检测。
1.创建Canvas,并设置其渲染模式
2.在Canvas下创建空对象BG,创建脚本GameItemSelect
3.在BG下方常见Text_Title;Btn_Left;Btn_Right;Btn_Select;
4.并为Btn_Left;Btn_Right;Btn_Select;添加BoxCollider组件
三、游戏项目搭建及切换
1.创建空对象,命名为GameItemSpawn,创建脚本GameItemSpawn。管理所有生成的GameItem游戏项目
2.创建空对象,命名为GameItem,在其下方创建一个Quad对象,并拖入Prefabs作为预制体
3.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GameItemSpawn : MonoBehaviour
{
public static GameItemSpawn _Instance;//单例模式
public Material[] m_GameItemMatArr;//游戏项目材质球
public GameObject GameItemPrefabs;//预制体游戏项目
private float m_Angle;//变化的角度
public int index;//各游戏项目的索引号
void Awake()
{
_Instance = this;
m_Angle = 360.0f / m_GameItemMatArr.Length;
for(int i=0;i< m_GameItemMatArr.Length; i++)
{
GameObject go=Instantiate(GameItemPrefabs, transform);
go.transform.localEulerAngles = new Vector3(0, i * m_Angle, 0);
go.GetComponentInChildren<MeshRenderer>().material= m_GameItemMatArr[i];
go.GetComponentInChildren<GameItemVideo>().SetVideoName(m_GameItemMatArr[i].name) ;//视频的名字和游戏项目材质球名字相同
go.GetComponentInChildren<GameItemVideo>().selectindex = i;//赋值选择的索引
}
}
//切换游戏项目右箭头
public void GoForward()
{
index++;
if(index> m_GameItemMatArr.Length)
{
index = 0;
}
transform.DORotate(new Vector3(0, -index * m_Angle, 0), 0.3f);
}
//切换游戏项目左箭头
public void GoBack()
{
index--;
if (index <0)
{
index = m_GameItemMatArr.Length-1;
}
transform.DORotate(new Vector3(0, -index * m_Angle, 0), 0.3f);
}
}
3.以GameItemSpawn为中心生成10个GameItem对象
四、与UI交互
1.GameItemSelect脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameItemSelect : MonoBehaviour
{
public Text Text_Title;
public Button btn_SelectLeft;
public Button btn_SelectRight;
public Button btn_SelectMiddle;
private string[] Tex_GameItemName;//游戏项目名字
private void Start()
{
GetGameItemNameText();
Init();
}
private void Update()
{
Text_Title.text = Tex_GameItemName[GameItemSpawn._Instance.index];
}
void Init()
{
btn_SelectLeft.onClick.AddListener(() =>
{
GameItemSpawn._Instance.GoBack();
});
btn_SelectRight.onClick.AddListener(() =>
{
GameItemSpawn._Instance.GoForward();
});
btn_SelectMiddle.onClick.AddListener(() =>
{
});
}
//获得游乐项目的名字文本
public void GetGameItemNameText()
{
TextAsset textasset=Resources.Load<TextAsset>("游乐项目名字");
Tex_GameItemName = textasset.text.Split(‘\n‘);//将名字用换行符分割返回字符串数组
}
}
注意:在Resources下方有一个游戏项目名字的.txt文件,打开文件另存为时,将编码改为UTF_8保存。
五、添加游戏项目视频
1.为GameItem预制体添加Video Player组件,并设置如图所示
2.创建脚本Game Item Video
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using VRTK;
using System.IO;
public class GameItemVideo : MonoBehaviour
{
public VideoPlayer m_VideoPlayer;//播放视频
public int selectindex;//选择的索引
void Start()
{
GameObject.Find("ControllerRight").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItemVideo_TouchpadReleased;
GameObject.Find("ControllerLeft").GetComponent<VRTK_ControllerEvents>().TouchpadReleased += GameItemVideo_TouchpadReleased;
}
private void Update()
{
if (selectindex == GameItemSpawn._Instance.index)
{
GetComponent<MeshCollider>().enabled = true;
}
else
{
GetComponent<MeshCollider>().enabled = false;
}
}
//圆盘按键抬起
private void GameItemVideo_TouchpadReleased(object sender, ControllerInteractionEventArgs e)
{
m_VideoPlayer.Pause();
}
//设置视频名称
public void SetVideoName(string videoName)
{
m_VideoPlayer.url = GetVideoPath(videoName);
}
//获得视频路径
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();
}
}
3.
原文:https://www.cnblogs.com/syq208/p/13958410.html