unity 使用相机中心出发ui事件 (头盔注视操作UI)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class RaycastUI : PointerInputModule
{
    public override void Process()
    {
        Handlock();
    }

    private PointerEventData pointData;
    private List<RaycastResult> result = new List<RaycastResult>();
    private RaycastResult currentResult;//
    private RaycastResult lastResult;

    private Coroutine cor;
    private  float waitTime=1.5f;
    public Image handler;//
    protected override void Awake()
    {
        base.Awake();
        StartCoroutine(Delay());
    }
    private void Handlock()
    {
        result.Clear();
        if (pointData == null)
            pointData = new PointerEventData(eventSystem);
        pointData.Reset();
        pointData.position = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
        eventSystem.RaycastAll(pointData, result);
        currentResult = pointData.pointerCurrentRaycast = FindFirstRaycast(result);
        if(currentResult .gameObject!=null)
        {
            //handler.gameObject.SetActive(true);
            //handler.transform.position = currentResult.gameObject.transform.position;
            if (currentResult.gameObject != lastResult.gameObject)
            {
                Debug.Log("新的");
                isDelay = true;
                //if (cor != null)
                //{
                //    StopCoroutine(Delay());
                //    cor = null;
                //}
                //cor = StartCoroutine(Delay());
            }
        }else
        {
            //if (cor != null)
            //{
            //    StopCoroutine(cor);
            //    cor = null;
            //}
            //StopAllCoroutines();
            isDelay = false;
            handler.fillAmount = 1;
        }
        lastResult = currentResult;
        ProcessMove(pointData);
    }

    private bool isDelay = false;
    IEnumerator Delay()
    {
        handler.fillAmount = 0;
        float time = 0;
        while (true)
        {
            if (isDelay)
            {
                time += Time.deltaTime;
                handler.fillAmount = time / waitTime;
                yield return null;
                if (time >=0.95f)
                {
                    ExecuteEvents.ExecuteHierarchy(currentResult.gameObject, pointData, ExecuteEvents.pointerClickHandler);
                    isDelay = false;
                    time = 0;
                    handler.fillAmount = 0;
                }
            }
            else
            {
                handler.fillAmount = 0;
                time = 0;
                yield return null;
            }
        }



        while (time<waitTime )
        {
            time += Time.deltaTime;
            handler.fillAmount = time / waitTime;
            yield return null;
        }
    }
}

 

将此脚本挂载在eventsystem上,禁用standalone inputmode

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值