using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ScreenCameraRay : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
///
/// 输入设备上一次检测到的目标物体
///
protected GameObject target;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//Debug.Log("点击屏幕");
CheckRay(Input.mousePosition, TouchPhase.Began);
}
else if(Input.GetMouseButtonUp(0))
{
CheckRay(Input.mousePosition, TouchPhase.Ended);
}
else if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
CheckRay(Input.GetTouch(0).position, TouchPhase.Began);
}
else if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
CheckRay(Input.GetTouch(0).position, TouchPhase.Ended);
}
}
else
{
CheckRay(Input.mousePosition, TouchPhase.Canceled);
}
}
private void CheckRay(Vector3 point, TouchPhase mode)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(point);
if (Physics.Raycast(ray, out hit))
{
if(!hit.transform.gameObject.Equals(target))
{
ExecuteEvents.Execute(target, null, (x, y) => x.OnPointerExit(null));
}
target = hit.transform.gameObject;
//CDebug.Log(hit.transform.name);
//Debug.Log(hit.transform.tag);
if(mode == TouchPhase.Began)
{
ExecuteEvents.Execute(hit.transform.gameObject, null, (x, y) => x.OnPointerDown(null));
ExecuteEvents.Execute(hit.transform.gameObject, null, (x, y) => x.OnPointerClick(null));
}
else if (mode == TouchPhase.Ended)
{
ExecuteEvents.Execute(hit.transform.gameObject, null, (x, y) => x.OnPointerUp(null));
}
}
}
}