2024-04-07 17:20:48 +08:00
|
|
|
|
using System;
|
2024-04-09 18:16:37 +08:00
|
|
|
|
using System.Collections.Generic;
|
2024-04-07 17:20:48 +08:00
|
|
|
|
using UnityEngine;
|
2024-04-09 18:16:37 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2024-04-07 17:20:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
namespace Game.RayCast
|
2024-04-07 17:20:48 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public class MouseInput : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public RayCastType rayCastType = RayCastType._2D;
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
MouseInputData mouseInputData = new MouseInputData();
|
2024-04-07 17:20:48 +08:00
|
|
|
|
|
2024-04-08 18:20:55 +08:00
|
|
|
|
// public event MouseInputEventHandle ev;
|
2024-04-07 17:20:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
private void Update()
|
2024-04-07 17:20:48 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
if (this.IsMouseOverUI()) return;
|
2024-04-07 17:20:48 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
switch (rayCastType)
|
|
|
|
|
{
|
|
|
|
|
case RayCastType._3D:
|
|
|
|
|
// 3d
|
|
|
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
|
RaycastHit hit;
|
|
|
|
|
if (Physics.Raycast(ray, out hit))
|
2024-04-07 17:20:48 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
if (hit.collider != null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(hit.collider.name);
|
|
|
|
|
}
|
2024-04-08 18:20:55 +08:00
|
|
|
|
}
|
2024-04-11 16:44:25 +08:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case RayCastType._2D:
|
|
|
|
|
//2d
|
|
|
|
|
var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
|
|
|
var hit2D = Physics2D.Raycast(point, -Vector2.up);
|
|
|
|
|
if (hit2D.collider != null)
|
2024-04-08 18:20:55 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.Log(hit2D.collider.name);
|
|
|
|
|
mouseInputData.rayCastType = this.rayCastType;
|
|
|
|
|
mouseInputData.point = hit2D.point;
|
|
|
|
|
mouseInputData.isPressDown = true;
|
|
|
|
|
mouseInputData.go = hit2D.collider.gameObject;
|
|
|
|
|
EventManager.Instance.FireNow(this, new InputObjectFinishEventArgs(mouseInputData));
|
|
|
|
|
}
|
|
|
|
|
else if (Input.GetMouseButtonDown(2))
|
|
|
|
|
{
|
2024-04-09 13:22:52 +08:00
|
|
|
|
// Game.bfsManager.Test(hit2D.point);
|
2024-04-11 16:44:25 +08:00
|
|
|
|
}
|
2024-04-07 17:20:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
2024-04-07 17:20:48 +08:00
|
|
|
|
}
|
2024-04-09 18:16:37 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
private bool IsMouseOverUI()
|
2024-04-09 18:16:37 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
PointerEventData eventData = new PointerEventData(EventSystem.current)
|
|
|
|
|
{
|
|
|
|
|
position = Input.mousePosition
|
|
|
|
|
};
|
2024-04-09 18:16:37 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
List<RaycastResult> results = new List<RaycastResult>();
|
|
|
|
|
EventSystem.current.RaycastAll(eventData, results);
|
2024-04-09 18:16:37 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
foreach (var result in results)
|
2024-04-09 18:16:37 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
// 检查射线检测到的对象是否为UI元素,并且是否属于CanvasGroup
|
|
|
|
|
CanvasGroup canvasGroup = result.gameObject.GetComponentInParent<CanvasGroup>();
|
|
|
|
|
|
|
|
|
|
if (canvasGroup != null)
|
2024-04-09 18:16:37 +08:00
|
|
|
|
{
|
2024-04-11 16:44:25 +08:00
|
|
|
|
// 如果CanvasGroup不允许交互或不阻挡射线,我们认为鼠标不是位于UI上
|
|
|
|
|
if (!canvasGroup.interactable || !canvasGroup.blocksRaycasts)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-04-09 18:16:37 +08:00
|
|
|
|
}
|
2024-04-11 16:44:25 +08:00
|
|
|
|
|
|
|
|
|
// 如果找到至少一个允许交互且阻挡射线的UI元素,返回true
|
|
|
|
|
return true;
|
2024-04-09 18:16:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
// 如果所有射线检测到的UI元素都不允许交互或不阻挡射线,返回false
|
|
|
|
|
return false;
|
2024-04-09 18:16:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public enum RayCastType
|
|
|
|
|
{
|
|
|
|
|
_3D,
|
|
|
|
|
_2D,
|
|
|
|
|
}
|
2024-04-08 18:20:55 +08:00
|
|
|
|
|
2024-04-11 16:44:25 +08:00
|
|
|
|
public class MouseInputData
|
|
|
|
|
{
|
|
|
|
|
public RayCastType rayCastType;
|
|
|
|
|
public bool isPressDown;
|
|
|
|
|
public Vector3 point;
|
|
|
|
|
public GameObject go;
|
|
|
|
|
}
|
2024-04-07 17:20:48 +08:00
|
|
|
|
}
|