InteractableButtonEvents.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Sends simple controller button events to UnityEvents
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( Interactable ) )]
  12. public class InteractableButtonEvents : MonoBehaviour
  13. {
  14. public UnityEvent onTriggerDown;
  15. public UnityEvent onTriggerUp;
  16. public UnityEvent onGripDown;
  17. public UnityEvent onGripUp;
  18. public UnityEvent onTouchpadDown;
  19. public UnityEvent onTouchpadUp;
  20. public UnityEvent onTouchpadTouch;
  21. public UnityEvent onTouchpadRelease;
  22. //-------------------------------------------------
  23. void Update()
  24. {
  25. for ( int i = 0; i < Player.instance.handCount; i++ )
  26. {
  27. Hand hand = Player.instance.GetHand( i );
  28. if ( hand.controller != null )
  29. {
  30. if ( hand.controller.GetPressDown( Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger ) )
  31. {
  32. onTriggerDown.Invoke();
  33. }
  34. if ( hand.controller.GetPressUp( Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger ) )
  35. {
  36. onTriggerUp.Invoke();
  37. }
  38. if ( hand.controller.GetPressDown( Valve.VR.EVRButtonId.k_EButton_Grip ) )
  39. {
  40. onGripDown.Invoke();
  41. }
  42. if ( hand.controller.GetPressUp( Valve.VR.EVRButtonId.k_EButton_Grip ) )
  43. {
  44. onGripUp.Invoke();
  45. }
  46. if ( hand.controller.GetPressDown( Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad ) )
  47. {
  48. onTouchpadDown.Invoke();
  49. }
  50. if ( hand.controller.GetPressUp( Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad ) )
  51. {
  52. onTouchpadUp.Invoke();
  53. }
  54. if ( hand.controller.GetTouchDown( Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad ) )
  55. {
  56. onTouchpadTouch.Invoke();
  57. }
  58. if ( hand.controller.GetTouchUp( Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad ) )
  59. {
  60. onTouchpadRelease.Invoke();
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }