C++ callback 结构体数组传到unity上层

运行场景:

最近在做unity sdk,所有的功能都要从C++底层开始封装成C#,遇到一个问题,C++ 如何回调自定义数据结构到unity上层,简单的类型,int,float都没问题。

C++ Code

//自定义数据结构
typedef struct PointInfo {
    float x;
    float y;
}PointInfo;

typedef void(__stdcall *OnTouchDownListener)(void *point,int a);

OnTouchDownListener onTouchDown;
extern "C" {
    int SetTouchListner(OnTouchDownListener listener) {
        onTouchDown = listener;
    }
    int StartCallBack(){

        PointInfo p[] = {
            {3,4},
            {33,44},
        };
        onTouchDown((void*)p, 2);//2代表数组的元素个数
    }
}

C# Code

//定义数据结构,跟c++ 一一对应
    [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
    public struct PointInfo {
        public float x;
        public float y;
    }

    //定义托管类型
    private delegate void DLLCplusTouchCallBack(IntPtr array, int length);
     //声明本地回调实例
    private DLLCplusTouchCallBack TouchDownCallBack;

    //定义native 设置回调接口
     [DllImport("Native-Unity",CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Ansi)]
    public static extern int SetTouchListner(Delegate listener);

    //测试回调功能接口
    [DllImport("Native-Unity")]
    public static extern int StartCallback();


        /*
        C#层回调函数会调用这个函数来获取真正的数据结构数组内容
    get the data from the Intptr where is from c++ dll. 

    */
    private static T[] GetNativeArray<T>(IntPtr array, int length)
    {
        T[] result = new T[length];

        int size = Marshal.SizeOf(typeof(T));
        if (IntPtr.Size == 4) //32位机器
        {
            // 32-bit system
            for (int i = 0; i < result.Length; i++)
            {

                result[i] = (T)Marshal.PtrToStructure(array, typeof(T));

                array = new IntPtr(array.ToInt32() + size);
            }
        }
        else//64位机器
        {
            // probably 64-bit system
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = (T)Marshal.PtrToStructure(array, typeof(T));
                array = new IntPtr(array.ToInt64() + size);
            }
        }
        return result;
    }
    /*
    上层c#回调函数,c++最终会调用这个函数
     touch down callback
         */
    public void DLLCplusCallBackTouchDown(IntPtr array, int length)
    {
        Debug.Log("Enter :DLLCplusCallBackTouchDown length = " + length);
        if (length >= 10)
            length = 10;
        PointInfo[] resultVertices = GetNativeArray<PointInfo>(array, length);

        if (resultVertices != null)
        {
        //在这边获取到数据
            for (int i = 0; i < length; i++)
            {
                Debug.Log("point: " + length + " x = " + resultVertices[i].x + "   point y = " + resultVertices[i].y);
            }
        }
    }


    void Start () {
        TouchDownCallBack = DLLCplusCallBackTouchDown;
        SetTouchListner(TouchDownCallBack);

        StartCallback();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值