-
Notifications
You must be signed in to change notification settings - Fork 1
/
OneHandedGestureRecognizer.cs
58 lines (46 loc) · 2.21 KB
/
OneHandedGestureRecognizer.cs
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
using UnityEngine;
using UnityEngine.XR.Hands;
using UnityEngine.XR.Hands.Gestures;
namespace LC.Interaction
{
/// A modified version of Unity's StaticHandGesture component with a few extra features
public class OneHandedGestureRecognizer : GestureRecognizerBase
{
[Tooltip("The hand tracking events component to subscribe to receive updated joint data to be used for gesture detection.")]
public XRHandTrackingEvents HandTrackingEvents;
[Tooltip("The hand shape or pose that must be detected for the gesture to be performed.")]
public ScriptableObject HandShapeOrPose;
[Tooltip("The target Transform to user for target conditions in the hand shape or pose.")]
public Transform TargetTransform;
[Tooltip("The interval at which the gesture detection is performed.")]
public float GestureDetectionInterval = 0.1f;
private XRHandShape _handShape;
private XRHandPose _handPose;
void OnEnable()
{
HandTrackingEvents.jointsUpdated.AddListener(OnJointsUpdated);
_handShape = HandShapeOrPose as XRHandShape;
_handPose = HandShapeOrPose as XRHandPose;
if (_handPose != null && _handPose.relativeOrientation != null)
_handPose.relativeOrientation.targetTransform = TargetTransform;
}
void OnDisable() => HandTrackingEvents.jointsUpdated.RemoveListener(OnJointsUpdated);
void OnJointsUpdated(XRHandJointsUpdatedEventArgs eventArgs)
{
if (!isActiveAndEnabled || Time.timeSinceLevelLoad < _timeOfLastConditionCheck + GestureDetectionInterval) {
return;
}
_rawDetected =
HandTrackingEvents.handIsTracked &&
_handShape != null && _handShape.CheckConditions(eventArgs) ||
_handPose != null && _handPose.CheckConditions(eventArgs);
if (_rawDetected) {
if (!_lastRawDetected) _detectedTime = Time.time;
} else {
if(_lastRawDetected) _undetectedTime = Time.time;
}
_lastRawDetected = _rawDetected;
_timeOfLastConditionCheck = Time.timeSinceLevelLoad;
}
}
}