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