代码检查:String.Format(format, ...) 只接受字符串文字和常量字符串
此检查会报告由 Burst 编译器编译的代码中, string.Format(...) 调用的 format 实参不是字符串文字、常量字符串或插值字符串的情况。
Burst 仅支持受限形式的 string.Format。 如果格式字符串来自可变字段、变量或其它非常量来源,此检查会发出警告。
示例
在此示例中,格式字符串存储在 static 字段中。 在 Burst 编译的代码中不支持此操作。
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
private static string format = "Value: {0}";
public void Execute()
{
// Reported: mutable format string is not supported
string.Format(format, 12);
}
}
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ExampleJob : IJob
{
public void Execute()
{
// Correct: Use a string literal directly
string.Format("Value: {0}", 12);
}
}
快速修复
此检查不提供专用快速修复。 请手动将 format 实参替换为字符串文字或 const 字符串。
2026年 5月 8日