Unity对结构体中的数组预设置大小
使用范围
通过与后端交互获得Byte[] 数组,转化为结构体时报错
MarshalDirectiveException: Structure field of type Single[] can’t be marshalled as LPArray
(wrapper other) tagInterToSJ.PtrToStructure(intptr,object)
原因是定义结构体的时候,结构体中有数组,但是由于Unity/C#的设定,9.0以下不能设置数组大小
可以
public struct tagInterToSJ
{
public float[] A;
}
不能
public struct tagInterToSJ
{
public float[] A = new float[2];
}
解决办法
public struct tagInterToSJ
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public float[] A;
}
2为数组长度,Marshal.PtrToStructure不再报错,问题解决
点个赞吧,老哥/姐,孩子快没动力了