/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using UnityEngine;
/// A gaze tracker.
public class GazeTracker : MonoBehaviour
{
/// The raycaster.
[SerializeField]
private NRPointerRaycaster m_Raycaster;
/// True if is enabled, false if not.
private bool m_IsEnabled;
/// Gets the camera center.
/// The camera center.
private Transform CameraCenter
{
get
{
return NRInput.CameraCenter;
}
}
/// Starts this object.
private void Start()
{
OnControllerStatesUpdated();
}
/// Executes the 'enable' action.
private void OnEnable()
{
NRInput.OnControllerStatesUpdated += OnControllerStatesUpdated;
}
/// Executes the 'disable' action.
private void OnDisable()
{
NRInput.OnControllerStatesUpdated -= OnControllerStatesUpdated;
}
/// Executes the 'controller states updated' action.
private void OnControllerStatesUpdated()
{
UpdateTracker();
}
/// Updates the tracker.
private void UpdateTracker()
{
if (CameraCenter == null)
return;
m_IsEnabled = (NRInput.RaycastersActive && NRInput.RaycastMode == RaycastModeEnum.Gaze);
m_Raycaster.gameObject.SetActive(m_IsEnabled);
if (m_IsEnabled)
{
transform.position = CameraCenter.position;
transform.rotation = CameraCenter.rotation;
}
}
}
}