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