Hi, I am relatively new to unity and ML2 along with working with c#. I've been just messing around with the unity the last couple days, and have been attempting to implement a feature in my app where one can change the color of a sphere, by focusing on it. I've used these as my references:
Eye Tracking - Unity | Magic Leap
Eye Tracking | MagicLeap Developer Documentation
I've tried to combine aspects of both code, while keeping in mind that the API has changed for eye tracking between ML1 and ML2. I've built my app and focused on a sphere, however the colors won't change as expected. I'd appreciate any sort of guidance. Thanks! My code is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using static UnityEngine.XR.MagicLeap.InputSubsystem.Extensions;
//adding my own
using UnityEngine.InputSystem;
using UnityEngine.UI;
using UnityEngine.XR;
using InputDevice = UnityEngine.XR.InputDevice;
using UnityEditor;
using static MagicLeapInputs;
using UnityEngine.Android;
public class EyeTracking : MonoBehaviour
{
public GameObject Camera;
public Material FocusedMaterial, NonFocusedMaterial;
private Vector3 _heading;
private MeshRenderer _meshRenderer;
//ml inputs storage variable
private MagicLeapInputs mlInputs;
//eye actions storage variable
private MagicLeapInputs.EyesActions eyesActions;
// other eye data storage variables
private InputDevice eyesDevice;
// Was EyeTracking permission granted by user
private bool permissionGranted = false;
private readonly MLPermissions.Callbacks permissionCallbacks = new MLPermissions.Callbacks();
private void Awake()
{
permissionCallbacks.OnPermissionGranted += OnPermissionGranted;
permissionCallbacks.OnPermissionDenied += OnPermissionDenied;
permissionCallbacks.OnPermissionDeniedAndDontAskAgain += OnPermissionDenied;
}
void Start()
{
mlInputs = new MagicLeapInputs();
mlInputs.Enable();
MLPermissions.RequestPermission(MLPermission.EyeTracking, permissionCallbacks);
transform.position = Camera.transform.position + Camera.transform.forward * 2.0f;
_meshRenderer= gameObject.GetComponent<MeshRenderer>();
}
private void OnDestroy()
{
mlInputs.Disable();
mlInputs.Dispose();
InputSubsystem.Extensions.MLEyes.StopTracking();
}
private void Update()
{
if (!permissionGranted)
{
return;
}
if (!eyesDevice.isValid)
{
this.eyesDevice = InputSubsystem.Utils.FindMagicLeapDevice(InputDeviceCharacteristics.EyeTracking | InputDeviceCharacteristics.TrackedDevice);
return;
}
var eyes = eyesActions.Data.ReadValue<UnityEngine.InputSystem.XR.Eyes>();
RaycastHit rayHit;
_heading = eyes.fixationPoint - Camera.transform.position;
// Use the proper material
if (Physics.Raycast(Camera.transform.position, _heading, out rayHit, 10.0f))
{
_meshRenderer.material = FocusedMaterial;
}
else
{
_meshRenderer.material = NonFocusedMaterial;
}
}
private void OnPermissionDenied(string permission)
{
MLPluginLog.Error($"{permission} denied, example won't function.");
}
private void OnPermissionGranted(string permission)
{
InputSubsystem.Extensions.MLEyes.StartTracking();
eyesActions = new MagicLeapInputs.EyesActions(mlInputs);
permissionGranted = true;
}
}