forked from zxl/Frame
1
0
Fork 0
Frame/Assets/Scripts/RayCast/MouseInput.cs

51 lines
1.4 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
{
public enum RayCastType
{
_3D,
_2D,
}
private RayCastType rayCastType = RayCastType._2D;
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);
EventManager.Instance.FireNow(this, new InputObjectFinishEventArgs(hit2D.collider));
}
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}