该脚本将凝视功能与本地UI事件绑定在一起
using Microsoft.MixedReality.Toolkit.Experimental.UI;
using Microsoft.MixedReality.Toolkit.Input;
using System;
using UnityEngine;
using UnityEngine.Events;
using static TMPro.TMP_InputField;
[RequireComponent(typeof(FocusHandler))]
public class GazeAuxiliary : MonoSingleton<GazeAuxiliary>
{
[SerializeField]
[Tooltip("Event which is triggered when focus begins.")]
private UnityEvent Eye_Event = new UnityEvent();
private FocusHandler focusHandler;
public UnityEvent Execute_event
{
get { return Eye_Event; }
set { Eye_Event = value; }
}
[Header("UI类型 ")]
public UIType iType;
public float time = 3;
private bool isRunTime = false;
protected override void Awake()
{
base.Awake();
focusHandler = GetComponent<FocusHandler>();
focusHandler.OnFocusEnterEvent.AddListener(OnEnterEyeEvent);
focusHandler.OnFocusExitEvent.AddListener(OnExitEyeEvent);
RegisterUIEvent(iType);
}
private void RegisterUIEvent(UIType iType)
{
switch (iType)
{
case UIType.Button:
Eye_Event = GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().OnClick;
break;
case UIType.InputFiler:
Execute_event.AddListener( ()=>GetComponent<MRTKTMPInputField>().ActivateInputField());
break;
default:
break;
}
}
private void OnEnterEyeEvent()
{
InitTime(true);
}
private void OnExitEyeEvent()
{
InitTime(false);
}
private void InitTime(bool isact)
{
time = 3;
isRunTime = isact;
}
private void FixedUpdate()
{
if (isRunTime)
{
time -= Time.deltaTime;
if (time <= 0)
{
if (Execute_event != null)
Execute_event.Invoke();
isRunTime = false;
time = 3;
}
}
}
}
public enum UIType
{
None,
Button,
InputFiler,
}