第一步,从AssetStore导入最新版VRTK插件,下载完毕后,点击All全部选中再点击Import导入。
第二步,在Project窗口下搜索SDKManger,将VRTK_SDKManager预制体拖入Hierachy窗口下。
第三步,新建两个空物体,分别命名为LeftController和RightController,点击Inspector窗口下的AddComponent搜索ControllerEvents,点击VRTK_ControllerEvents把脚本挂载到空物体上。
第四步,点击VRTK_SDKMnager,将两个Controller拖入VRTK_SDKManager—ScriptAliases下,不设置这一步的话,Controller无法自动找到两个手柄物体。
第五步,新建一个脚本,命名为InputManager,新建一个空物体,命名为InputManager,将脚本挂载到物体上。
第六步,键入以下代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class InputManager : MonoBehaviour
{
//声明两个手柄的控制器
public VRTK_ControllerEvents LeftController;
public VRTK_ControllerEvents RightController;
// Use this for initialization
void Start () {
//添加一个触发事件,Triggerpressed表示扳机键被按压时触发
RightController.TriggerPressed += DoTriggerPressed;
}
// Update is called once per frame
void Update () {
}
void DoTriggerPressed(object obj, ControllerInteractionEventArgs e)
{
Debug.Log("TriggerPressed");
}
}
第六步,将Controller拖到脚本上
至此,基本设置已经完成,然后点击运行,按照Game窗口左上角的提示进行操作,按住LeftAlt控制手柄(默认是右手柄),按下鼠标右键,则触发了脚本中的DoTriggerPressed方法。
手柄上所有按键的触发事件委托,可以在VRTKExample_ControllerEventsDelegateListeners脚本中查看。
VRTK如果要用在SteamVR上面,不能直接从AssetStore上面下载,最高只能使用SteamVR1.2.3版本。
附上SteamVR1.2.3的下载链接