123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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()
- {
-
- }
- /// <summary>
- /// 输入设备上一次检测到的目标物体
- /// </summary>
- 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<IPointerExitHandler>(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<IPointerDownHandler>(hit.transform.gameObject, null, (x, y) => x.OnPointerDown(null));
- ExecuteEvents.Execute<IPointerClickHandler>(hit.transform.gameObject, null, (x, y) => x.OnPointerClick(null));
- }
- else if (mode == TouchPhase.Ended)
- {
- ExecuteEvents.Execute<IPointerUpHandler>(hit.transform.gameObject, null, (x, y) => x.OnPointerUp(null));
- }
- }
- }
- }
|