代码检查:不支持 'foreach' 构造
此检查会报告由 Burst 编译器 编译的代码中出现的 foreach 循环。
即使遍历如本地容器等适用于 Burst 的数据,Burst 也不支持 foreach 构造。
示例
在此示例中,使用 foreach 循环来遍历 Burst 编译的作业中的 NativeArray<int>。 不支持此操作,会被标记出来。
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public NativeArray<int> values;
public void Execute()
{
// Reported: foreach is not supported in Burst
foreach (var value in values)
{
}
}
}
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public NativeArray<int> values;
public void Execute()
{
// Correct: Replace foreach with an index-based loop
for (var i = 0; i < values.Length; i++)
{
var value = values[i];
}
}
}
2026年 5月 8日