Unity结构体中报错alDirectiveException: Structure field of type Single[] can‘t be marshalled as LPArray

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不再报错,问题解决

点个赞吧,老哥/姐,孩子快没动力了

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值