一、导入VR插件
这里用到的插件是 SteamVR 1.2.3 与 VRTK 3.3.0
在unity中首先导入SteamVR 1.2.3,再导入VRTK 3.3.0,可以避免一些错误
可以直接拖动unitypackage包到unity中,也可以通过窗口Assets->Import Package->CustomPackage,选择要导入的内容,会弹出以下界面,直接点击Import按钮即可
另外要在ProjectSettings中设置一下,导入XR
二、使用插件搭建VR环境
找到 [VRTK_SDKManager] 预制件,将其拖入场景中,接着可以按照下图中这种层级,创建空物体
在LeftHand和RightHand物体上面添加基本的组件,如下图所示,如果有UI界面,则需要在Canvas上添加 VRTK_UICanvas 组件
然后将LeftHand和RightHand分别放到[VRTK_SDKManager] 的组件中,如下图所示
再创建一个脚本放置到LeftHand和RightHand上面,来控制交互实现
例如我这里写了一个HandPressed.cs脚本文章来源:https://www.toymoban.com/news/detail-789759.html
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class HandPressed : MonoBehaviour
{
private VRTK_ControllerEvents vrtk_Controller;
private VRTK_Pointer vrtk_Point;
void Start()
{
vrtk_Controller = GetComponent<VRTK_ControllerEvents>();//获取手柄事件组件
vrtk_Controller.TriggerPressed += VRTK_Controller_TriggerPressed;//按下扳机键
vrtk_Controller.TriggerReleased += VRTK_Controller_TriggerReleased;//松开扳机键
vrtk_Controller.ButtonTwoPressed += VRTK_Controller_ButtonTwoPressed;//按下菜单键
vrtk_Controller.TouchpadPressed += VRTK_Controller_TouchpadPressed;//按下圆盘键
vrtk_Controller.TouchpadReleased += VRTK_Controller_TouchpadRelease;//松开圆盘键
vrtk_Point = GetComponent<VRTK_Pointer>();//获取射线组件
vrtk_Point.DestinationMarkerHover += VRTKPoint_DestinationMarkerHover;//射线触碰事件
}
/// <summary>
/// 手柄圆盘键松开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTK_Controller_TouchpadRelease(object sender, ControllerInteractionEventArgs e)
{
//这里写手柄圆盘键松开时要响应的事件
}
/// <summary>
/// 手柄圆盘键按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTK_Controller_TouchpadPressed(object sender, ControllerInteractionEventArgs e)
{
//这里写手柄圆盘键按下时要响应的事件
}
/// <summary>
/// 手柄射线触碰
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTKPoint_DestinationMarkerHover(object sender, DestinationMarkerEventArgs e)
{
}
/// <summary>
/// 手柄菜单键按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTK_Controller_ButtonTwoPressed(object sender, ControllerInteractionEventArgs e)
{
//这里写手柄菜单键按下时要响应的事件
}
/// <summary>
/// 手柄扳机键松开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTK_Controller_TriggerReleased(object sender, ControllerInteractionEventArgs e)
{
//这里写手柄扳机键松开时要响应的事件
}
/// <summary>
/// 手柄扳机键按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void VRTK_Controller_TriggerPressed(object sender, ControllerInteractionEventArgs e)
{
//这里写手柄扳机键按下时要响应的事件
}
}
以上即是实现交互的基本步骤,如有需完善或错误的地方,欢迎指正文章来源地址https://www.toymoban.com/news/detail-789759.html
到了这里,关于使用Unity开发VR基本步骤的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!