2
0

UIElement.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: UIElement that responds to VR hands and generates UnityEvents
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using UnityEngine.UI;
  9. using System;
  10. namespace Valve.VR.InteractionSystem
  11. {
  12. //-------------------------------------------------------------------------
  13. [RequireComponent( typeof( Interactable ) )]
  14. public class UIElement : MonoBehaviour
  15. {
  16. public CustomEvents.UnityEventHand onHandClick;
  17. protected Hand currentHand;
  18. //-------------------------------------------------
  19. protected virtual void Awake()
  20. {
  21. Button button = GetComponent<Button>();
  22. if ( button )
  23. {
  24. button.onClick.AddListener( OnButtonClick );
  25. }
  26. }
  27. //-------------------------------------------------
  28. protected virtual void OnHandHoverBegin( Hand hand )
  29. {
  30. currentHand = hand;
  31. InputModule.instance.HoverBegin( gameObject );
  32. ControllerButtonHints.ShowButtonHint( hand, hand.uiInteractAction);
  33. }
  34. //-------------------------------------------------
  35. protected virtual void OnHandHoverEnd( Hand hand )
  36. {
  37. InputModule.instance.HoverEnd( gameObject );
  38. ControllerButtonHints.HideButtonHint( hand, hand.uiInteractAction);
  39. currentHand = null;
  40. }
  41. //-------------------------------------------------
  42. protected virtual void HandHoverUpdate( Hand hand )
  43. {
  44. if ( hand.uiInteractAction != null && hand.uiInteractAction.GetStateDown(hand.handType) )
  45. {
  46. InputModule.instance.Submit( gameObject );
  47. ControllerButtonHints.HideButtonHint( hand, hand.uiInteractAction);
  48. }
  49. }
  50. //-------------------------------------------------
  51. protected virtual void OnButtonClick()
  52. {
  53. onHandClick.Invoke( currentHand );
  54. }
  55. }
  56. #if UNITY_EDITOR
  57. //-------------------------------------------------------------------------
  58. [UnityEditor.CustomEditor( typeof( UIElement ) )]
  59. public class UIElementEditor : UnityEditor.Editor
  60. {
  61. //-------------------------------------------------
  62. // Custom Inspector GUI allows us to click from within the UI
  63. //-------------------------------------------------
  64. public override void OnInspectorGUI()
  65. {
  66. DrawDefaultInspector();
  67. UIElement uiElement = (UIElement)target;
  68. if ( GUILayout.Button( "Click" ) )
  69. {
  70. InputModule.instance.Submit( uiElement.gameObject );
  71. }
  72. }
  73. }
  74. #endif
  75. }