2
0

InteractableExample.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Sample
  9. {
  10. //-------------------------------------------------------------------------
  11. [RequireComponent( typeof( Interactable ) )]
  12. public class InteractableExample : MonoBehaviour
  13. {
  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. // Save our position/rotation so that we can restore it when we detach
  55. oldPosition = transform.position;
  56. oldRotation = transform.rotation;
  57. // Call this to continue receiving HandHoverUpdate messages,
  58. // and prevent the hand from hovering over anything else
  59. hand.HoverLock(interactable);
  60. // Attach this object to the hand
  61. hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
  62. }
  63. else if (isGrabEnding)
  64. {
  65. // Detach this object from the hand
  66. hand.DetachObject(gameObject);
  67. // Call this to undo HoverLock
  68. hand.HoverUnlock(interactable);
  69. // Restore position/rotation
  70. transform.position = oldPosition;
  71. transform.rotation = oldRotation;
  72. }
  73. }
  74. //-------------------------------------------------
  75. // Called when this GameObject becomes attached to the hand
  76. //-------------------------------------------------
  77. private void OnAttachedToHand( Hand hand )
  78. {
  79. generalText.text = string.Format("Attached: {0}", hand.name);
  80. attachTime = Time.time;
  81. }
  82. //-------------------------------------------------
  83. // Called when this GameObject is detached from the hand
  84. //-------------------------------------------------
  85. private void OnDetachedFromHand( Hand hand )
  86. {
  87. generalText.text = string.Format("Detached: {0}", hand.name);
  88. }
  89. //-------------------------------------------------
  90. // Called every Update() while this GameObject is attached to the hand
  91. //-------------------------------------------------
  92. private void HandAttachedUpdate( Hand hand )
  93. {
  94. generalText.text = string.Format("Attached: {0} :: Time: {1:F2}", hand.name, (Time.time - attachTime));
  95. }
  96. private bool lastHovering = false;
  97. private void Update()
  98. {
  99. if (interactable.isHovering != lastHovering) //save on the .tostrings a bit
  100. {
  101. hoveringText.text = string.Format("Hovering: {0}", interactable.isHovering);
  102. lastHovering = interactable.isHovering;
  103. }
  104. }
  105. //-------------------------------------------------
  106. // Called when this attached GameObject becomes the primary attached object
  107. //-------------------------------------------------
  108. private void OnHandFocusAcquired( Hand hand )
  109. {
  110. }
  111. //-------------------------------------------------
  112. // Called when another attached GameObject becomes the primary attached object
  113. //-------------------------------------------------
  114. private void OnHandFocusLost( Hand hand )
  115. {
  116. }
  117. }
  118. }