我需要一个内存缓冲区,用于在内存中保存实际对象,而不是引用。
我正在为我的应用程序使用 Unity Engine。为了在 Unity 中提交绘制调用,必须在函数调用中提供网格对象和材质对象。这是我正在尝试优化的算法的一部分。
Mesh[] meshes;
Material[] materials;
Foreach (mesh-type)
DrawMeshInstancedIndirect(mesh[i],material[i]...etc);
我希望能够迭代直接包含对象数据的内存缓冲区,没有引用,因为我不想用无用的数据填充我的CPU缓存,以获取内存随机部分中的每个对象。您不能用结构体替换这些特殊的 Unity 类。
无论这是不好的做法还是非常混乱的解决方案都没关系。这是我的应用程序中性能关键的部分,其中结构不能替代类。
这里是对绘制调用函数的引用,以了解更多上下文:
Graphics.DrawMeshInstancedIndirect(Unity 脚本 API)
尝试使用
fixed
关键字。
class Foo
{
public int[] stuff = new int[999];
public unsafe void ExamplePinned()
{
fixed (int* pStuff = stuff)
{
//Alright, the data array is all set and ready to go. Think of 'pStuff' as a pointer
//to the first element in the array. You can use it to access the memory of the
//array and copy it to another buffer
}
}
}