第一步,從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的下載鏈接