using System; using UnityEngine; namespace Game.RayCast; public class MouseInput : MonoBehaviour { public RayCastType rayCastType = RayCastType._2D; MouseInputData mouseInputData = new MouseInputData(); // public event MouseInputEventHandle ev; 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); 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); } } break; default: throw new ArgumentOutOfRangeException(); } } } public enum RayCastType { _3D, _2D, } public class MouseInputData { public RayCastType rayCastType; public bool isPressDown; public Vector3 point; public GameObject go; }