其他分享
首页 > 其他分享> > Hololens 凝视 延迟调用

Hololens 凝视 延迟调用

作者:互联网

该脚本将凝视功能与本地UI事件绑定在一起

using Microsoft.MixedReality.Toolkit.Experimental.UI;
using Microsoft.MixedReality.Toolkit.Input;
using System;
using UnityEngine;
using UnityEngine.Events;
using static TMPro.TMP_InputField;

/// <summary>
/// 用于凝视辅助
/// </summary>
[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,
}

标签:UIType,Hololens,void,private,public,凝视,using,Event,延迟
来源: https://blog.csdn.net/weixin_44876165/article/details/121213764