当前位置:   article > 正文

Unity与VRTK的自定义按键事件_unity steamvr 手柄任意按键按下事件

unity steamvr 手柄任意按键按下事件

不想用VRTK插件封装好的手柄按键事件的话,就必须在手柄控制器上挂上SteamVR_TrackedObject脚本,挂上改脚本后运行项目,会自动在手柄控制器生成SteamVR_Controller脚本。

 

 脚本挂好了后,可以开始写一个自己自定义控制手柄的脚本VR_Controller.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Valve.VR;
  6. public class VRController : MonoBehaviour
  7. {
  8. private SteamVR_TrackedObject leftTrackedObj;
  9. private SteamVR_TrackedObject rightTrackedObj;
  10. void Awake()
  11. {
  12. }
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. OnScenesStart();
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. }
  22. void FixedUpdate()
  23. {
  24. LeftHandEvenes();
  25. RightHandEvents();
  26. }
  27. /// <summary>
  28. /// Scene initialization
  29. /// </summary>
  30. private void OnScenesStart()
  31. {
  32. transform.GetComponent<MeshRenderer>().enabled = false;
  33. transform.GetComponent<SteamVR_PlayArea>().enabled = false;
  34. leftTrackedObj = transform.GetChild(0).GetComponent<SteamVR_TrackedObject>();
  35. rightTrackedObj = transform.GetChild(1).GetComponent<SteamVR_TrackedObject>();
  36. }
  37. #region HandEvents
  38. /// <summary>
  39. /// Left hand events
  40. /// </summary>
  41. private void LeftHandEvenes()
  42. {
  43. if (transform.GetChild(0).gameObject.activeSelf)
  44. {
  45. var device = SteamVR_Controller.Input((int)leftTrackedObj.index);
  46. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
  47. {
  48. print("左手柄按下Trigger键");
  49. }
  50. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
  51. {
  52. print("左手柄按下Grip键");
  53. }
  54. if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  55. {
  56. print("左手柄按下ApplicationMenu键");
  57. }
  58. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
  59. {
  60. print("左手柄按下Touchpad键");
  61. }
  62. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
  63. {
  64. print("左手柄触摸Touchpad键");
  65. }
  66. if (device.GetTouchDown(Valve.VR.EVRButtonId.k_EButton_A))
  67. {
  68. print("左手柄触摸什么键");
  69. }
  70. }
  71. }
  72. }

以上脚本基本包含了手柄所有事件触发事件,如有需要还可以加一些组合键。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/146553
推荐阅读
相关标签
  

闽ICP备14008679号