InteractableExample.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Demonstrates how to create a simple interactable object
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( Interactable ) )]
  12. public class InteractableExample : MonoBehaviour
  13. {
  14. private TextMesh textMesh;
  15. private Vector3 oldPosition;
  16. private Quaternion oldRotation;
  17. private float attachTime;
  18. private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & ( ~Hand.AttachmentFlags.SnapOnAttach ) & ( ~Hand.AttachmentFlags.DetachOthers );
  19. //-------------------------------------------------
  20. void Awake()
  21. {
  22. textMesh = GetComponentInChildren<TextMesh>();
  23. textMesh.text = "No Hand Hovering";
  24. }
  25. //-------------------------------------------------
  26. // Called when a Hand starts hovering over this object
  27. //-------------------------------------------------
  28. private void OnHandHoverBegin( Hand hand )
  29. {
  30. textMesh.text = "Hovering hand: " + hand.name;
  31. }
  32. //-------------------------------------------------
  33. // Called when a Hand stops hovering over this object
  34. //-------------------------------------------------
  35. private void OnHandHoverEnd( Hand hand )
  36. {
  37. textMesh.text = "No Hand Hovering";
  38. }
  39. //-------------------------------------------------
  40. // Called every Update() while a Hand is hovering over this object
  41. //-------------------------------------------------
  42. private void HandHoverUpdate( Hand hand )
  43. {
  44. if ( hand.GetStandardInteractionButtonDown() || ( ( hand.controller != null ) && hand.controller.GetPressDown( Valve.VR.EVRButtonId.k_EButton_Grip ) ) )
  45. {
  46. if ( hand.currentAttachedObject != gameObject )
  47. {
  48. // Save our position/rotation so that we can restore it when we detach
  49. oldPosition = transform.position;
  50. oldRotation = transform.rotation;
  51. // Call this to continue receiving HandHoverUpdate messages,
  52. // and prevent the hand from hovering over anything else
  53. hand.HoverLock( GetComponent<Interactable>() );
  54. // Attach this object to the hand
  55. hand.AttachObject( gameObject, attachmentFlags );
  56. }
  57. else
  58. {
  59. // Detach this object from the hand
  60. hand.DetachObject( gameObject );
  61. // Call this to undo HoverLock
  62. hand.HoverUnlock( GetComponent<Interactable>() );
  63. // Restore position/rotation
  64. transform.position = oldPosition;
  65. transform.rotation = oldRotation;
  66. }
  67. }
  68. }
  69. //-------------------------------------------------
  70. // Called when this GameObject becomes attached to the hand
  71. //-------------------------------------------------
  72. private void OnAttachedToHand( Hand hand )
  73. {
  74. textMesh.text = "Attached to hand: " + hand.name;
  75. attachTime = Time.time;
  76. }
  77. //-------------------------------------------------
  78. // Called when this GameObject is detached from the hand
  79. //-------------------------------------------------
  80. private void OnDetachedFromHand( Hand hand )
  81. {
  82. textMesh.text = "Detached from hand: " + hand.name;
  83. }
  84. //-------------------------------------------------
  85. // Called every Update() while this GameObject is attached to the hand
  86. //-------------------------------------------------
  87. private void HandAttachedUpdate( Hand hand )
  88. {
  89. textMesh.text = "Attached to hand: " + hand.name + "\nAttached time: " + ( Time.time - attachTime ).ToString( "F2" );
  90. }
  91. //-------------------------------------------------
  92. // Called when this attached GameObject becomes the primary attached object
  93. //-------------------------------------------------
  94. private void OnHandFocusAcquired( Hand hand )
  95. {
  96. }
  97. //-------------------------------------------------
  98. // Called when another attached GameObject becomes the primary attached object
  99. //-------------------------------------------------
  100. private void OnHandFocusLost( Hand hand )
  101. {
  102. }
  103. }
  104. }