---
title: 优化射线投射和OtherPhysics查询
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/physics-section/physics-overview/physics-optimization/memory/raycasts-queries
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/physics-section/physics-overview/physics-optimization/memory/raycasts-queries.md
last_updated: 2026-10-30
generated_at: 2026-11-17T10:35:40.648Z
locale: zh-cn
---

# 优化射线投射和OtherPhysics查询

> 通过使用高效的查询版本和批次理来优化Physics查询性性能并减少垃圾Collection开销。

通过使用高效的查询版本和批次理来优化Physics查询性性能并减少垃圾Collection开销。

## 使用非分配查询版本##use-non-allocating-query-versions

标准 Physics 查询，例如 [`Physics.Raycast`](/engine/6000.0/script-reference/unityengine/physics/raycast.md)、[`Physics.SphereCast`](/engine/6000.0/script-reference/unityengine/physics/spherecast.md) 或 [`Physics.OverlapSphere`](/engine/6000.0/script-reference/unityengine/physics/overlapsphere.md)，会为查询结果分配 ON 内存。此分配可能会导致垃圾Collection暂停，尤其是在频繁调用它们的情况下。

相反，应使用这些查询的非分配对应项，这些对应项会将查询结果写入您提供的预分配 Array。非分配对应项可能包括：

* [`Physics.RaycastNonAlloc`](/engine/6000.0/script-reference/unityengine/physics/raycastnonalloc.md)
* [`Physics.SphereCastNonAlloc`](/engine/6000.0/script-reference/unityengine/physics/spherecastnonalloc.md)
* [`Physics.OverlapSphereNonAlloc`](/engine/6000.0/script-reference/unityengine/physics/overlapspherenonalloc.md)

例如：

```csharp
int hitCount = Physics.RaycastNonAlloc(ray, preAllocatedHitsArray, distance, layerMask);
```

使用非分配查询When，必须提供预分配数Array。例如，`RaycastHit[] preAllocatedHitsArray = new RaycastHit[10];`。

适当大小此数Array的大小：make其足够大以捕获典型情况下的最大预期命中，但不要太大以浪费内存。如果找到的实际碰撞体数量超过缓冲区区数Array的大小，则仅返回不超过Array容量的结果，REST结果将被忽略。

> **Tip:**
>
> 选择在典型需求与内存注意事项之间取得平衡的缓冲区大小。对游戏进行配置文件以了解常见的点击计数。

## 对多个操作使用批次查询##use-batch-queries-for-multiple-operations

running许多单独的Physics查询（例如，每帧许多射线投射）可能会因为每次调用的开销而降低性能。如果需要同时执行多个查询，请使用 API（如 [`RaycastCommand`](/engine/6000.0/script-reference/unityengine/raycastcommand.md)、[`SpherecastCommand`](/engine/6000.0/script-reference/unityengine/spherecastcommand.md) 或 [`BoxcastCommand`](/engine/6000.0/script-reference/unityengine/boxcastcommand.md)）以及[任务系统](/engine/6000.0/manual/scripting/optimization/job-system.md)对这些查询进行批次。

此方法利用多线程并行处理查询，显著提高批量查询操作的性能。When有许多独立的射线投射（或Other查询）可以并行处理时，此功能尤其强大。

要使用批次查询，请执行以下步骤：

1. 使用 All raycasts 的参数创建并填充`NativeArray<RaycastCommand>`。
2. 分配一个`NativeArray<RaycastHit>`来存储结果。
3. 使用 `RaycastCommand.ScheduleBatch` 调度批次处理，返回 `JobHandle`。
4. 然后，您可以执行其他工作，稍后使用 `jobHandle.Complete` 确保任务完成，然后再访问结果。例如，确保在 Next 帧的 Start 或 When needed 时完成任务。

## 其他资源##additional-resources

* [Unity Profiler](/engine/6000.0/manual/analysis/profiler.md)
* [管理时间和帧率](/engine/6000.0/manual/scripting/object-oriented-development/managing-time-and-frame-rate.md)
* [内存性能分析器](https://docs.unity3d.com/Packages/com.unity.memoryprofiler@latest)
* [Physics 项目设置](/engine/6000.0/manual/unity-editor/editor-settings-reference/comp-manager-group/class-physics-manager.md)
* [RaycastCommand](/engine/6000.0/script-reference/unityengine/raycastcommand.md)
* [使用任务系统编写多线程代码](/engine/6000.0/manual/scripting/optimization/job-system.md)
* [Collections 包](https://docs.unity3d.com/Packages/com.unity.collections@latest?subfolder=/manual/index.html)
