代码检查:不支持从非只读静态字段加载
此检查会报告在由 Burst 编译器编译的代码中读取非 readonly 的静态字段或静态自动属性的情况。
Burst 可读取常量和 readonly 静态数据,但不支持加载可变静态状态。
示例
在此示例中, 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;
}
}
快速修复
此检查不提供专用的快速修复。 请通过移除对可变静态的读取或使数据兼容 Burst(例如,使用只读静态数据、常量或将值传入作业)手动修复此问题。
2026年 5月 8日