From ea665c58cd57ff25240f461a496bd2050a8e85ff Mon Sep 17 00:00:00 2001 From: MuHua-123 <136542559+MuHua-123@users.noreply.github.com> Date: Wed, 24 Dec 2025 16:06:51 +0800 Subject: [PATCH] Update RayTool.cs --- Packages/Tools/Runtime/ModuleTools/RayTool.cs | 216 +++++++++--------- 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/Packages/Tools/Runtime/ModuleTools/RayTool.cs b/Packages/Tools/Runtime/ModuleTools/RayTool.cs index c10f012..39e975f 100644 --- a/Packages/Tools/Runtime/ModuleTools/RayTool.cs +++ b/Packages/Tools/Runtime/ModuleTools/RayTool.cs @@ -3,118 +3,118 @@ using System.Collections.Generic; using UnityEngine; namespace MuHua { - public static class RayTool { - public static RaycastHit hitInfo; - public static readonly LayerMask DefaultLayerMask = ~(1 << 0) | 1 << 0; + public static class RayTool { + public static RaycastHit hitInfo; + public static readonly LayerMask DefaultLayerMask = ~(1 << 0) | 1 << 0; - /// 鼠标坐标转世界坐标 - public static bool GetMouseToWorldPosition(out Vector3 position) { - return GetScreenToWorldPosition(Input.mousePosition, out position); - } - /// 鼠标坐标转世界坐标 - public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position) { - return GetScreenToWorldPosition(camera, Input.mousePosition, out position); - } - /// 鼠标坐标转世界坐标 - public static bool GetMouseToWorldPosition(out Vector3 position, LayerMask planeLayerMask) { - return GetScreenToWorldPosition(Input.mousePosition, out position, planeLayerMask); - } - /// 鼠标坐标转世界坐标 - public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position, LayerMask planeLayerMask) { - return GetScreenToWorldPosition(camera, Input.mousePosition, out position, planeLayerMask); - } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(out Vector3 position) { + return GetScreenToWorldPosition(Input.mousePosition, out position); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position) { + return GetScreenToWorldPosition(camera, Input.mousePosition, out position); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(Input.mousePosition, out position, planeLayerMask); + } + /// 鼠标坐标转世界坐标 + public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(camera, Input.mousePosition, out position, planeLayerMask); + } - /// 屏幕坐标转世界坐标 - public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position) { - return GetScreenToWorldPosition(screen, out position, DefaultLayerMask); - } - /// 屏幕坐标转世界坐标 - public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position) { - return GetScreenToWorldPosition(camera, screen, out position, DefaultLayerMask); - } - /// 屏幕坐标转世界坐标 - public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position, LayerMask planeLayerMask) { - return GetScreenToWorldPosition(Camera.main, screen, out position, planeLayerMask); - } - /// 屏幕坐标转世界坐标 - public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position, LayerMask planeLayerMask) { - Ray ray = camera.ScreenPointToRay(screen); - Physics.Raycast(ray, out hitInfo, 200, planeLayerMask); - position = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); - if (hitInfo.transform != null) { position = hitInfo.point; } - return hitInfo.transform != null; - } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position) { + return GetScreenToWorldPosition(screen, out position, DefaultLayerMask); + } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position) { + return GetScreenToWorldPosition(camera, screen, out position, DefaultLayerMask); + } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position, LayerMask planeLayerMask) { + return GetScreenToWorldPosition(Camera.main, screen, out position, planeLayerMask); + } + /// 屏幕坐标转世界坐标 + public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position, LayerMask planeLayerMask) { + Ray ray = camera.ScreenPointToRay(screen); + Physics.Raycast(ray, out hitInfo, 500, planeLayerMask); + position = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); + if (hitInfo.transform != null) { position = hitInfo.point; } + return hitInfo.transform != null; + } - /// 从鼠标坐标获取对象 - public static bool GetMouseToWorldObject(out T value) where T : Object { - return GetScreenToWorldObject(Input.mousePosition, out value); - } - /// 从鼠标坐标获取对象 - public static bool GetMouseToWorldObject(Camera camera, out T value) where T : Object { - return GetScreenToWorldObject(camera, Input.mousePosition, out value); - } - /// 从鼠标坐标获取对象 - public static bool GetMouseToWorldObject(out T value, LayerMask planeLayerMask) where T : Object { - return GetScreenToWorldObject(Input.mousePosition, out value, planeLayerMask); - } - /// 从鼠标坐标获取对象 - public static bool GetMouseToWorldObject(Camera camera, out T value, LayerMask planeLayerMask) where T : Object { - return GetScreenToWorldObject(camera, Input.mousePosition, out value, planeLayerMask); - } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(out T value) where T : Object { + return GetScreenToWorldObject(Input.mousePosition, out value); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(Camera camera, out T value) where T : Object { + return GetScreenToWorldObject(camera, Input.mousePosition, out value); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(Input.mousePosition, out value, planeLayerMask); + } + /// 从鼠标坐标获取对象 + public static bool GetMouseToWorldObject(Camera camera, out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(camera, Input.mousePosition, out value, planeLayerMask); + } - /// 从屏幕坐标获取对象 - public static bool GetScreenToWorldObject(Vector3 screen, out T value) where T : Object { - return GetScreenToWorldObject(screen, out value, DefaultLayerMask); - } - /// 从屏幕坐标获取对象 - public static bool GetScreenToWorldObject(Camera camera, Vector3 screen, out T value) where T : Object { - return GetScreenToWorldObject(camera, screen, out value, DefaultLayerMask); - } - /// 从屏幕坐标获取对象 - public static bool GetScreenToWorldObject(Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object { - return GetScreenToWorldObject(Camera.main, screen, out value, planeLayerMask); - } - /// 从屏幕坐标获取对象 - public static bool GetScreenToWorldObject(Camera camera, Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object { - Ray ray = camera.ScreenPointToRay(screen); - Physics.Raycast(ray, out hitInfo, 200, planeLayerMask); - value = hitInfo.transform?.GetComponent(); - return value != null; - } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Vector3 screen, out T value) where T : Object { + return GetScreenToWorldObject(screen, out value, DefaultLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Camera camera, Vector3 screen, out T value) where T : Object { + return GetScreenToWorldObject(camera, screen, out value, DefaultLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object { + return GetScreenToWorldObject(Camera.main, screen, out value, planeLayerMask); + } + /// 从屏幕坐标获取对象 + public static bool GetScreenToWorldObject(Camera camera, Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object { + Ray ray = camera.ScreenPointToRay(screen); + Physics.Raycast(ray, out hitInfo, 500, planeLayerMask); + value = hitInfo.transform?.GetComponent(); + return value != null; + } - /// 从鼠标坐标获取碰撞信息 - public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo) { - return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo); - } - /// 从鼠标坐标获取碰撞信息 - public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo) { - return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo); - } - /// 从鼠标坐标获取碰撞信息 - public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo, LayerMask planeLayerMask) { - return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo, planeLayerMask); - } - /// 从鼠标坐标获取碰撞信息 - public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo, LayerMask planeLayerMask) { - return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo, planeLayerMask); - } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo, planeLayerMask); + } + /// 从鼠标坐标获取碰撞信息 + public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo, planeLayerMask); + } - /// 从屏幕坐标获取碰撞信息 - public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo) { - return GetScreenToWorldHitInfo(screen, out hitInfo, DefaultLayerMask); - } - /// 从屏幕坐标获取碰撞信息 - public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo) { - return GetScreenToWorldHitInfo(camera, screen, out hitInfo, DefaultLayerMask); - } - /// 从屏幕坐标获取碰撞信息 - public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) { - return GetScreenToWorldHitInfo(Camera.main, screen, out hitInfo, planeLayerMask); - } - /// 从屏幕坐标获取碰撞信息 - public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) { - Ray ray = camera.ScreenPointToRay(screen); - return Physics.Raycast(ray, out hitInfo, 200, planeLayerMask); - } - } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(screen, out hitInfo, DefaultLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo) { + return GetScreenToWorldHitInfo(camera, screen, out hitInfo, DefaultLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) { + return GetScreenToWorldHitInfo(Camera.main, screen, out hitInfo, planeLayerMask); + } + /// 从屏幕坐标获取碰撞信息 + public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) { + Ray ray = camera.ScreenPointToRay(screen); + return Physics.Raycast(ray, out hitInfo, 500, planeLayerMask); + } + } } \ No newline at end of file