Frame/Assets/Scripts/RayCast/MouseInput.cs

71 lines
2.0 KiB
C#
Raw Normal View History

2024-04-07 17:20:48 +08:00
using System;
using UnityEngine;
namespace Game.RayCast;
public class MouseInput : MonoBehaviour
{
2024-04-08 18:20:55 +08:00
public RayCastType rayCastType = RayCastType._2D;
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
private void Update()
{
switch (rayCastType)
{
case RayCastType._3D:
// 3d
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
Debug.Log(hit.collider.name);
}
}
break;
case RayCastType._2D:
//2d
var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var hit2D = Physics2D.Raycast(point, -Vector2.up);
if (hit2D.collider != null)
{
if (Input.GetMouseButtonDown(0))
{
UnityEngine.Debug.Log(hit2D.collider.name);
2024-04-08 18:20:55 +08:00
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))
{
Game.bfsManager.Test(hit2D.point);
2024-04-07 17:20:48 +08:00
}
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
2024-04-08 18:20:55 +08:00
}
public enum RayCastType
{
_3D,
_2D,
}
public class MouseInputData
{
public RayCastType rayCastType;
public bool isPressDown;
public Vector3 point;
public GameObject go;
2024-04-07 17:20:48 +08:00
}