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