一、在Unity中使用c++代码
Unity想调用C++代码,则需要c++开发人员打包成so库。
在Unity中通过DllImport,和dll一样调用。
需要注意的点:
C++代码需要extern"C"来封装成dll
因为unity默认使用c语言调用外部接口,会对c++代码进行命名矫正,使用extern"C"避免这个问题。
so在Unity中的存放路径为Plugins/Android/Libs/arm64-v8a
二、参数传递
1.结构体需对照数据类型创建。
2.参数个数和格式要一一对应,int16使用short,int32使用long, int8使用byte。
3.结构体数组,在向so传递时,使用IntPtr
三、调用用例,添加回调
回调方法需要添加声明[UnmanagedFunctionPointer(CallingConvention.cdecl)]
[DlLImport(dlName:"soTest",CallingConvention = CallingConvention.Cdect)]publi static extern bool soMethod(string str, string num, Callback callBack);
[UnmanagedFunctionPointer(CallingConvention.cdecl)]
public delegate void Callback(string msg);
四、传递结构体数组情况
1. 结构体定义与内存对齐
C++端:
cpp
#pragma pack(push, 1) // 强制1字节对齐,避免编译器填充字节[1,9](@ref)
struct MyStruct {int id;float value;char name[32]; // 固定长度字符数组
};
#pragma pack(pop)// 导出函数声明(确保符号可见)
extern "C" __attribute__((visibility("default")))
void ProcessStructArray(MyStruct* arr, int size);
C#端:
csharp
[StructLayout(LayoutKind.Sequential, Pack = 1)] // 与C++内存布局一致[1,9](@ref)
public struct MyStruct {public int id;public float value;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] // 固定长度字符串映射[4,11](@ref)public string name;
}
2.C#端分配非托管内存并传递
csharp
[DllImport("MyLib", CallingConvention = CallingConvention.Cdecl)]
public static extern void ProcessStructArray(IntPtr structArray, int size);void SendData() {MyStruct[] data = new MyStruct[3];// 填充数据...// 分配非托管内存int structSize = Marshal.SizeOf(typeof(MyStruct));IntPtr buffer = Marshal.AllocHGlobal(structSize * data.Length);// 逐个复制结构体到内存[1,5](@ref)for (int i = 0; i < data.Length; i++) {IntPtr ptr = new IntPtr(buffer.ToInt64() + i * structSize);Marshal.StructureToPtr(data[i], ptr, false);}// 调用C++函数ProcessStructArray(buffer, data.Length);// 释放内存(必须手动释放)Marshal.FreeHGlobal(buffer);
}
3. C++端接收并处理数据
cpp
void ProcessStructArray(MyStruct* arr, int size) {for (int i = 0; i < size; i++) {MyStruct& s = arr[i];// 修改数据(如数值运算)s.value *= 2.0f;printf("ID: %d, Name: %s\n", s.id, s.name);}
}
4.关键注意事项
内存对齐问题
C#必须使用[StructLayout(LayoutKind.Sequential, Pack = 1)],
C++需用#pragma pack强制对齐,确保双方结构体大小和字段偏移一致。
验证工具:通过Marshal.OffsetOf(typeof(MyStruct), “field”)检查偏移量。
字符串处理
C#字符串需用[MarshalAs(UnmanagedType.ByValTStr)]映射为固定长度字符数组,避免C++端缓冲区溢出。
函数导出与平台差异
Linux/Mac的.so库需以lib前缀命名(如libMyLib.so),C#的DllImport需省略前缀。
使用nm -D libMyLib.so检查导出符号是否存在。
内存管理
必须显式释放:Marshal.AllocHGlobal分配的内存需调用FreeHGlobal,否则导致内存泄漏。
避免野指针:传递完成后C++不应保留对内存的引用。