public static int RaycastNonAlloc (Vector2 origin, Vector2 direction, RaycastHit2D[] results, float distance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, float minDepth= -Mathf.Infinity, float maxDepth= Mathf.Infinity);

Parameters

minDepth仅包括 Z 坐标(深度)大于或等于该值的对象。
maxDepth仅包括 Z 坐标(深度)小于或等于该值的对象。
origin射线在 2D 空间中的起点。
directionA vector representing the direction of the ray.
results用于接收结果的数组。
distanceThe maximum distance over which to cast the ray.
layerMask筛选器,用于检查仅在指定层上的对象。

Returns

int 返回放置在 results 数组中的结果数。

Description

Casts a ray into the Scene.

此函数类似于 RaycastAll 函数,不同之处在于其结果会返回到提供的数组中。整数返回值是与该线条交叠的对象数(可能为零),但如果结果数组中没有足够的元素来报告所有这些结果,则不会调整该数组的大小。其意义在于不为这些结果分配内存,因此,如果经常进行光线投射,可以提高垃圾回收性能。

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

另请参阅:LayerMask 类、RaycastHit2D 类、RaycastLinecastDefaultRaycastLayersIgnoreRaycastLayerraycastsHitTriggers