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

Posted 哇小明

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    

以上是关于C++ callback 结构体数组传到unity上层的主要内容,如果未能解决你的问题,请参考以下文章

C++ 如何将一个静态结构体数组初始化?

C#如何调用C++的DLL的结构体数组指针

C++结构体指针数组如何分配空间,用new

c++ sort 函数能对类或者结构体进行排序吗

C++基础3(函数指针结构体)

C++基础3(函数指针结构体)