首先,我要说的是,我没有被卡住,因为我有很多解决方法!最好的一点是我不再需要 1Byte Pack 了:P
但老实说,我对这种行为感到很困惑。我正在使用 Unity,它在 Windows 上完美运行,但在 Android(单声道)上失败
- 我写了什么无效的东西吗?
- 我是否在 Mono 上出现了错误?!
评论绝对欢迎!
public class TEST
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct MyStruct {
public float field1;
public float field2;
};
//This order sucess on windows, but failed on android
private byte oneByte = 0x00;
private MyStruct mMyStruct;
public bool doTest()
{
try {
mMyStruct.field1 = (oneByte++);
} catch {
// System.NullReferenceException: Object reference not set to an instance of an object
return false;
}
return true;
}
}
一些额外的信息/线索:
Android/Windows 之间的差异可能来自处理器架构差异 Android ARM(RISC),假设 4Bytes alignement (或至少 2Bytes in thumb mode)
[StructLayout(LayoutKind.Sequential, Pack = 1)] 似乎影响的不仅仅是结构。
内存对齐可能变成:
[ onByte ] [ field1 #0 ] [ field1 #1 ] [ field1 #2 ]
[ field1 #3 ] [ field2 #1 ] [ field2 #1 ] [ field2 #3 ]
[ field2 #4 ] [ *pad* ] [ *pad* ] [ *pad* ]
我期待更多这样的东西:
[ onByte ] [ *pad* ] [ *pad* ] [ *pad* ]
[ field1 #0 ] [ field1 #1 ] [ field1 #2 ] [ field1 #3 ]
[ field2 #1 ] [ field2 #1 ] [ field2 #3 ][ field2 #4 ]