ManoMotion Unity SDK 2.0
SDK documentation
Loading...
Searching...
No Matches
Code Examples

Button interaction with hand tracking

Clicking button with HandMouseController example

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using ManoMotion;
public class HandMouseController : MonoBehaviour
{
// Cursors to display where pinch is aiming.
[SerializeField] RectTransform[] cursors;
private void Update()
{
bool hasHandCursor = false;
for (int i = 0; i < handInfos.Length; i++)
{
{
// Make sure the hand is pinching.
if (handInfo.gestureInfo.manoGestureContinuous != ManoGestureContinuous.OPEN_PINCH_GESTURE)
continue;
Vector3 screenPosition = GetCursorScreenPosition(i);
cursors[i].position = screenPosition;
// Check the gesture trigger.
bool clicked = handInfos[i].gestureInfo.manoGestureTrigger.Equals(ManoGestureTrigger.CLICK);
HandleInteractionUI(screenPosition, clicked);
hasHandCursor = true;
}
}
// No hand was found, stop hovering any previously hovered object.
if (!hasHandCursor)
{
EventSystem.current.SetSelectedGameObject(null);
}
}
// Returns a screen position between the thumb tip and index finger tip.
private Vector3 GetCursorScreenPosition(int handIndex)
{
// Get the world space positions from the SkeletonManager that already applies smoothing.
List<GameObject> joints = handIndex == 0 ? SkeletonManager.instance.joints : SkeletonManager.instance.jointsSecond;
Vector3 pos1 = joints[4].transform.position;
Vector3 pos2 = joints[8].transform.position;
Vector3 middle = (pos1 + pos2) / 2;
Vector3 position = Camera.main.WorldToScreenPoint(middle);
return position;
}
private bool HandleInteractionUI(Vector3 screenPosition, bool click)
{
// Use the EventSystem to find UI objects
PointerEventData pointerEventData = new PointerEventData(EventSystem.current) { position = screenPosition };
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerEventData, results);
// Look for the first Button component.
foreach (RaycastResult result in results)
{
if (result.gameObject.TryGetComponent(out Button button))
{
EventSystem.current.SetSelectedGameObject(button.gameObject);
if (click)
{
button.onClick.Invoke();
}
return true;
}
}
// If no Button was found then stop current selection.
EventSystem.current.SetSelectedGameObject(null);
return false;
}
}
HandInfo[] HandInfos
Definition ManoMotionManagerBase.cs:81
bool TryGetHandInfo(LeftOrRightHand leftRight, out HandInfo handInfo)
Returns true and gives back the hand info of the left/right hand specified.
Definition ManoMotionManagerBase.cs:306
The ManomotionManager handles the communication with the SDK.
Definition ManoMotionManager.cs:13
static ManoMotionManager Instance
Definition ManoMotionManager.cs:19
Handles the visualization of the skeleton joints.
Definition SkeletonManager.cs:16
List< GameObject > jointsSecond
Definition SkeletonManager.cs:22
static SkeletonManager instance
Definition SkeletonManager.cs:40
Definition CameraChangeListener.cs:5
LeftOrRightHand
Definition GestureInfo.cs:58
ManoGestureContinuous
Similar to Manoclass Continuous Gestures are Gesture Information that is being updated on every frame...
Definition GestureInfo.cs:47
ManoGestureTrigger
Trigger Gestures are a type of Gesture Information retrieved for a given frame when the user perfoms ...
Definition GestureInfo.cs:30
ManoGestureTrigger manoGestureTrigger
Trigger gestures are those that happen in one frame.
Definition GestureInfo.cs:165
Contains information about the hand.
Definition HandInfo.cs:23
GestureInfo gestureInfo
Information about hand gestures.
Definition HandInfo.cs:32