在unity Burst编译的函数中,是否能直接使用外部静态变量
- User
在unity Burst编译的函数中,是否能直接使用外部静态变量
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
在unity Burst编译的函数中,是否能直接使用外部静态变量
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
通常情况下,不能直接在 Unity Burst 编译的函数(例如 C# Job System 中的 Job 或使用 [BurstCompile] 标记的函数)中访问任意的外部托管 (managed) 静态变量。
原因如下:
例外和替代方案:
static readonly 常量(基本类型/Blittable 类型): 对于 static readonly 字段,如果它们是基本类型(int, float 等)或 Blittable 结构体(只包含 Blittable 类型的结构体),并且其值在编译时已知,Burst 编译器可能能够将它们的值直接内联到编译后的代码中。这实际上不是在运行时访问静态变量,而是使用了它的编译时常量值。
public static class MyConstants
{
public static readonly int MaxIterations = 100;
public static readonly float Gravity = -9.81f;
}
[BurstCompile]
struct MyJob : IJob
{
public void Execute()
{
// Burst MAY be able to inline these values
int iterations = MyConstants.MaxIterations;
float g = MyConstants.Gravity;
// ... use iterations and g ...
}
}
static readonly,如果类型不是 Blittable 的(例如包含引用类型或 bool 的结构体),或者初始化逻辑复杂到 Burst 无法在编译时确定其值,访问也可能会失败。SharedStatic<T>: 这是 Burst 专门提供的用于在 Burst 代码和 C# 代码之间共享静态数据的类型。你需要显式地创建和初始化 SharedStatic<T> 实例。
using Unity.Burst;
using Unity.Collections;
using UnityEngine;
public static class GlobalBurstData
{
// SharedStatic requires initialization, often via RuntimeInitializeOnLoadMethod
public static SharedStatic<float> SharedGravity = SharedStatic<float>.GetOrCreate<GlobalBurstData, SharedGravityKey>();
// A dummy struct or class to uniquely identify the SharedStatic field
private class SharedGravityKey {}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void Initialize()
{
SharedGravity.Data = -9.81f;
}
}
[BurstCompile]
struct MyBurstJob : IJob
{
public NativeArray<float> velocities;
public void Execute()
{
// Access the data via SharedStatic
float gravity = GlobalBurstData.SharedGravity.Data;
for (int i = 0; i < velocities.Length; i++)
{
velocities[i] += gravity * 0.1f; // Example usage
}
}
}
SharedStatic<T> 中的 T 必须是 Burst 兼容的类型(Blittable)。通过 Job 数据传递: 最推荐、最安全、也最符合 DOTS 设计模式的方法是不使用全局静态变量,而是将所需的数据作为 Job 结构体的成员传递进去。
[BurstCompile]
struct MyJobWithData : IJob
{
// Pass the needed "global" value into the job
[ReadOnly] public float GravityValue;
public NativeArray<float> velocities;
public void Execute()
{
for (int i = 0; i < velocities.Length; i++)
{
velocities[i] += GravityValue * 0.1f;
}
}
}
// --- How to schedule it ---
public class JobScheduler : MonoBehaviour
{
public float currentGravity = -9.81f;
public NativeArray<float> velocityData;
void Start() {
velocityData = new NativeArray<float>(10, Allocator.Persistent);
}
void Update()
{
var job = new MyJobWithData
{
GravityValue = this.currentGravity, // Get value from regular C# code
velocities = this.velocityData
};
job.Schedule().Complete();
}
void OnDestroy() {
if(velocityData.IsCreated) velocityData.Dispose();
}
}
总结:
static readonly 的基本/Blittable 类型常量可能会被内联。SharedStatic<T>。如果你尝试在 Burst 代码中访问不允许的静态变量,Burst 编译器通常会报错,或者你可以在 Burst Inspector 窗口中看到相关的错误信息。