InteractableExample.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. namespace Valve.VR.InteractionSystem.Sample
  8. {
  9. //-------------------------------------------------------------------------
  10. [RequireComponent( typeof( Interactable ) )]
  11. public class InteractableExample : MonoBehaviour
  12. {
  13. public Transform camera;
  14. private TextMesh generalText;
  15. private TextMesh hoveringText;
  16. private Vector3 oldPosition;
  17. private Quaternion oldRotation;
  18. private float attachTime;
  19. private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & ( ~Hand.AttachmentFlags.SnapOnAttach ) & (~Hand.AttachmentFlags.DetachOthers) & (~Hand.AttachmentFlags.VelocityMovement);
  20. private Interactable interactable;
  21. //-------------------------------------------------
  22. void Awake()
  23. {
  24. //var textMeshs = GetComponentsInChildren<TextMesh>();
  25. //generalText = textMeshs[0];
  26. //hoveringText = textMeshs[1];
  27. //generalText.text = "No Hand Hovering";
  28. //hoveringText.text = "Hovering: False";
  29. interactable = this.GetComponent<Interactable>();
  30. }
  31. //-------------------------------------------------
  32. // Called when a Hand starts hovering over this object
  33. //-------------------------------------------------
  34. private void OnHandHoverBegin( Hand hand )
  35. {
  36. //generalText.text = "Hovering hand: " + hand.name;
  37. }
  38. //-------------------------------------------------
  39. // Called when a Hand stops hovering over this object
  40. //-------------------------------------------------
  41. private void OnHandHoverEnd( Hand hand )
  42. {
  43. //generalText.text = "No Hand Hovering";
  44. }
  45. //-------------------------------------------------
  46. // Called every Update() while a Hand is hovering over this object
  47. //-------------------------------------------------
  48. private void HandHoverUpdate( Hand hand )
  49. {
  50. GrabTypes startingGrabType = hand.GetGrabStarting();
  51. bool isGrabEnding = hand.IsGrabEnding(this.gameObject);
  52. if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)
  53. {
  54. transform.SetParent(null);
  55. // Save our position/rotation so that we can restore it when we detach
  56. oldPosition = transform.position;
  57. oldRotation = transform.rotation;
  58. // Call this to continue receiving HandHoverUpdate messages,
  59. // and prevent the hand from hovering over anything else
  60. hand.HoverLock(interactable);
  61. // Attach this object to the hand
  62. hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
  63. }
  64. else if (isGrabEnding)
  65. {
  66. // Detach this object from the hand
  67. hand.DetachObject(gameObject);
  68. // Call this to undo HoverLock
  69. hand.HoverUnlock(interactable);
  70. // Restore position/rotation
  71. //transform.position = oldPosition;
  72. transform.SetParent(GameObject.Find("Automatic Cameras").transform);
  73. }
  74. }
  75. //-------------------------------------------------
  76. // Called when this GameObject becomes attached to the hand
  77. //-------------------------------------------------
  78. private void OnAttachedToHand( Hand hand )
  79. {
  80. //generalText.text = string.Format("Attached: {0}", hand.name);
  81. attachTime = Time.time;
  82. }
  83. //-------------------------------------------------
  84. // Called when this GameObject is detached from the hand
  85. //-------------------------------------------------
  86. private void OnDetachedFromHand( Hand hand )
  87. {
  88. //generalText.text = string.Format("Detached: {0}", hand.name);
  89. }
  90. //-------------------------------------------------
  91. // Called every Update() while this GameObject is attached to the hand
  92. //-------------------------------------------------
  93. private void HandAttachedUpdate( Hand hand )
  94. {
  95. //generalText.text = string.Format("Attached: {0} :: Time: {1:F2}", hand.name, (Time.time - attachTime));
  96. }
  97. private bool lastHovering = false;
  98. private void Update()
  99. {
  100. /*if (interactable.isHovering != lastHovering) //save on the .tostrings a bit
  101. {
  102. //hoveringText.text = string.Format("Hovering: {0}", interactable.isHovering);
  103. lastHovering = interactable.isHovering;
  104. }*/
  105. }
  106. //-------------------------------------------------
  107. // Called when this attached GameObject becomes the primary attached object
  108. //-------------------------------------------------
  109. private void OnHandFocusAcquired( Hand hand )
  110. {
  111. }
  112. //-------------------------------------------------
  113. // Called when another attached GameObject becomes the primary attached object
  114. //-------------------------------------------------
  115. private void OnHandFocusLost( Hand hand )
  116. {
  117. }
  118. }
  119. }