Skip to content

Commit

Permalink
增加鼠标射线检测
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoKyaku committed Dec 8, 2023
1 parent aaa7577 commit 1416ded
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 114 deletions.
13 changes: 9 additions & 4 deletions Megumin.Explosion/Megumin/Class/Pool.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
#if !MEGUMIN_EXPLOSION_EXTERNAL_DLL //引用外部dll时忽略编译,用于Unity工程和外部工程共享代码

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Collections;

namespace System
namespace Megumin
{
/// <summary>
/// 对象池核心容器
Expand Down Expand Up @@ -414,4 +415,8 @@ public class ConcurrentHashSetPool<T> : ConcurrentStackPoolCore<HashSet<T>>
}
}

#endif




Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Megumin
{
public abstract class MouseRayPicker<T> : MonoBehaviour
{
public float maxDistance = float.PositiveInfinity;
public GameObjectFilter filter;
public LayerMask LayerMask = -5;
public Camera Camera;

public bool TryPick(out T result)
{
return TryPick(out result, out var _, out var _);
}

public bool TryPick(out T result, out Ray ray, out RaycastHit hitInfo)
{
if (!Camera)
{
Camera = Camera.main;
}

return TryPick(out result, Camera, out ray, out hitInfo);
}

public bool TryPick(out T result, Camera camera, out Ray ray, out RaycastHit hitInfo)
{
if (!camera)
{
result = default;
ray = default;
hitInfo = default;
return false;
}

ray = camera.MouseRay();
return ray.TryPick(out result, out hitInfo, maxDistance, LayerMask, filter);
}


public bool TryPickAll(List<T> results)
{
return TryPickAll(results, out var _);
}

public bool TryPickAll(List<T> results, out Ray ray)
{
if (!Camera)
{
Camera = Camera.main;
}

return TryPickAll(results, Camera, out ray);
}

public bool TryPickAll(List<T> results, Camera camera, out Ray ray)
{
if (!camera)
{
ray = default;
return false;
}

ray = camera.MouseRay();
return ray.TryPickAll(results, maxDistance, LayerMask, filter);
}
}


}


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class NavMeshAreaPick : MonoBehaviour
{
[AreaMask]
public int mask = -1;
public LayerMask rayMask = 0b0111_1111_1111_1111_1111_1111_1111_1111;
public LayerMask LayerMask = -5;

[ProtectedInInspector]
public Camera Camera;
Expand Down Expand Up @@ -75,7 +75,7 @@ void Update()

HitAreaName = "";
NavMeshPosition = null;
if (Physics.Raycast(ray, out RaycastHit hitInfo, rayMask))
if (Physics.Raycast(ray, out RaycastHit hitInfo, LayerMask))
{
if (DrawRayHitPoint)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.InputSystem;

public static class CameraExtension_BA2750CDA0DB4FDEA611922CCD6F7C3E
{

public static Ray MouseRay(this Camera camera)
{
if (camera == null)
{
camera = Camera.main;
}

#if ENABLE_INPUT_SYSTEM
var mousePosition = Mouse.current.position.ReadValue();
#else
var mousePosition = Input.mousePosition;
#endif

var ray = camera.ScreenPointToRay(mousePosition);
return ray;
}

/// <summary>
/// 世界中一个点是否在相机内
/// </summary>
Expand Down
Loading

0 comments on commit 1416ded

Please sign in to comment.