InteractableExampleMod.cs 5.2 KB

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