代码检查:不支持从非只读 static 字段加载
此检查会报告在由 Burst 编译器 编译的代码中读取非 readonly 的 static 字段或 static 自动属性。
Burst 可以读取常量和 readonly static 数据,但不支持加载可变 static 状态。
示例
在此示例中, counter 字段被声明为 static ,但不是 readonly。 在 Burst 编译的作业中读取该字段是不支持的。
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
private static int counter = 1;
public void Execute()
{
// Reported: loading a non-readonly static field is not supported
var value = counter;
}
}
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public int counter;
public void Execute()
{
// Correct: Pass the value into the job
var value = counter;
}
}
快速修复
此检查不提供专用快速修复。 请手动修复,可以通过移除可变 static 读取,或使数据兼容 Burst(例如,使用 readonly static 数据、常量,或将值传递到作业中)。
2026年 5月 8日