Skip to content

Instantly share code, notes, and snippets.

@camnewnham
Last active April 2, 2024 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camnewnham/335e69074b170ceffc6b3809a046fe98 to your computer and use it in GitHub Desktop.
Save camnewnham/335e69074b170ceffc6b3809a046fe98 to your computer and use it in GitHub Desktop.
Configures leap motion to work in the editor
using Leap.Unity;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
/// <summary>
/// Allows using the Ultraleap in Desktop Mode for editor debugging, rather than on a headset.
/// Local position and rotation control the offset of the device.
/// For setup, see https://docs.ultraleap.com/xr-and-tabletop/xr/unity/getting-started/index.html
/// In Project Settings > UltraLeap, "Enable Leap XRHands Subsystem"
/// </summary>
public class DesktopLeapXRServiceProvider : LeapXRServiceProvider
{
[SerializeField, HideInInspector]
private bool needsAutoConfiguration = true;
private Transform cameraProxy;
#if UNITY_EDITOR
protected override void Update()
{
cameraProxy.SetPositionAndRotation(Camera.main.transform.position, Camera.main.transform.rotation);
base.Update();
}
protected override void Start()
{
cameraProxy = new GameObject("Leap Camera Proxy").transform;
cameraProxy.SetParent(transform.parent);
transform.SetParent(cameraProxy, true);
base.Start();
StartCoroutine(ApplyConfigurationCoroutine());
}
private IEnumerator ApplyConfigurationCoroutine()
{
ApplyConfigurationOverrides();
yield return new WaitWhile(() => _leapController == null || !_leapController.IsConnected);
_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP, _currentDevice);
_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD, _currentDevice);
}
private void ApplyConfigurationOverrides()
{
_preventInitializingTrackingMode = true;
editTimePose = Leap.TestHandFactory.TestHandPose.DesktopModeA;
deviceOffsetMode = DeviceOffsetMode.Transform;
_trackingOptimization = TrackingOptimizationMode.Desktop;
deviceOrigin = transform;
PositionDeviceRelativeToMainCamera = false;
}
private void OnDrawGizmosSelected()
{
if (needsAutoConfiguration)
{
Debug.Log($"Configured {nameof(DesktopLeapXRServiceProvider)}");
ApplyConfigurationOverrides();
transform.localPosition = new Vector3(0, -0.5f, 0.5f);
transform.localRotation = Quaternion.Euler(270, 180, 0);
needsAutoConfiguration = false;
EditorUtility.SetDirty(this);
}
base.OnEnable();
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment