赞
踩
不想用VRTK插件封装好的手柄按键事件的话,就必须在手柄控制器上挂上SteamVR_TrackedObject脚本,挂上改脚本后运行项目,会自动在手柄控制器生成SteamVR_Controller脚本。
脚本挂好了后,可以开始写一个自己自定义控制手柄的脚本VR_Controller.cs
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Valve.VR;
-
- public class VRController : MonoBehaviour
- {
- private SteamVR_TrackedObject leftTrackedObj;
- private SteamVR_TrackedObject rightTrackedObj;
-
- void Awake()
- {
-
- }
-
- // Start is called before the first frame update
- void Start()
- {
- OnScenesStart();
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-
- void FixedUpdate()
- {
- LeftHandEvenes();
- RightHandEvents();
- }
-
- /// <summary>
- /// Scene initialization
- /// </summary>
- private void OnScenesStart()
- {
- transform.GetComponent<MeshRenderer>().enabled = false;
- transform.GetComponent<SteamVR_PlayArea>().enabled = false;
-
- leftTrackedObj = transform.GetChild(0).GetComponent<SteamVR_TrackedObject>();
- rightTrackedObj = transform.GetChild(1).GetComponent<SteamVR_TrackedObject>();
- }
-
- #region HandEvents
- /// <summary>
- /// Left hand events
- /// </summary>
- private void LeftHandEvenes()
- {
- if (transform.GetChild(0).gameObject.activeSelf)
- {
- var device = SteamVR_Controller.Input((int)leftTrackedObj.index);
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
- {
- print("左手柄按下Trigger键");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
- {
- print("左手柄按下Grip键");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
- {
- print("左手柄按下ApplicationMenu键");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
- {
- print("左手柄按下Touchpad键");
- }
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
- {
- print("左手柄触摸Touchpad键");
- }
- if (device.GetTouchDown(Valve.VR.EVRButtonId.k_EButton_A))
- {
- print("左手柄触摸什么键");
- }
- }
- }
- }
以上脚本基本包含了手柄所有事件触发事件,如有需要还可以加一些组合键。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。