在作业中异步执行射线投射命令时用于设置该命令的结构。
当您使用此结构安排一批射线投射时,它们将异步执行且相互并行。射线投射的结果将写入结果缓冲区。由于结果是异步写入的,因此在作业完成之前无法访问结果缓冲区。
命令缓冲区中在索引 N 处的命令的结果将存储在结果缓冲区中索引为 N * maxHits 的位置。
如果 maxHits 大于命令的实际结果数量,结果缓冲区中将包含一些未击中任何目标的无效结果。第一个无效结果的识别依据是碰撞体为 null。Raycast 命令不会写入第二个及以后的无效结果,因此无法保证它们的碰撞体为 null。在遍历结果时,应该在找到第一个无效结果时停止循环。
Raycast 命令还控制是否让触发器碰撞体和背面三角形生成一个击中。如果将 hitMultipleFaces 设置为 true,Raycast 命令将返回每个三角网格的多次击中。您应该相应地调整 maxHits 和结果数组大小以存储所有击中。对于实体对象(球体、胶囊、方框、凸包),这最多返回一个结果。使用 QueryParameters 控制击中标志。
注意:只有 BatchQuery.ExecuteRaycastJob 会记录到分析器中。不会记录查询计数信息。
其他资源:Physics.Raycast、Physics.RaycastAll。
using Unity.Collections; using Unity.Jobs; using UnityEngine;
public class RaycastExample : MonoBehaviour { private void Start() { // Perform a single raycast using RaycastCommand and wait for it to complete // Setup the command and result buffers var results = new NativeArray<RaycastHit>(2, Allocator.TempJob);
var commands = new NativeArray<RaycastCommand>(1, Allocator.TempJob);
// Set the data of the first command Vector3 origin = Vector3.forward * -10;
Vector3 direction = Vector3.forward;
commands[0] = new RaycastCommand(origin, direction, QueryParameters.Default);
// Schedule the batch of raycasts. JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 1, 2, default(JobHandle));
// Wait for the batch processing job to complete handle.Complete();
// Copy the result. If batchedHit.collider is null there was no hit foreach (var hit in results) { if (hit.collider != null) { // If hit.collider is not null means there was a hit } }
// Dispose the buffers results.Dispose(); commands.Dispose(); } }
direction | 射线的朝向。 |
distance | 射线应检查碰撞的最大距离。 |
from | 射线在世界坐标中的起点。 |
physicsScene | 运行此命令的物理场景。 |
queryParameters | 用于指定批量查询附加参数的结构,如图层掩码、击中多个网格面、击中触发器和击中背面。 |
RaycastCommand | 创建射线投射命令。 |
ScheduleBatch | 调度一批要在作业中执行的射线投射。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.