目录
一、项目背景
最近在做一款虚拟人驱动项目,算法数据是以C++ SDK的形式封装起来的,那么这就涉及到了unity调用C++ dll。
二、C++SDK的封装
这个项目之前本人是有C++ SDK封装经验的,所以一些细节以及遵循的原则就不记录了。
1.第一版SDK
在和算法老师联调的过程中,封装的第一版dll的头文件SDKCaller.h是这样的:
#pragma once
#include <string.h>
#ifdef WIN32
#ifdef DLL_EXPORTS
#define EXPORT_CLASS __declspec(dllexport)
#define EXPORT_API extern "C" __declspec(dllexport)
#define EXPORT_CLASS_API
#else
#define EXPORT_CLASS __declspec(dllimport)
#define EXPORT_API extern "C" __declspec(dllimport)
#define EXPORT_CLASS_API
#endif
#else
#define EXPORT_CLASS
#define EXPORT_API extern "C" __attribute__((visibility("default")))
#define EXPORT_CLASS_API __attribute__((visibility("default")))
#endif
class EXPORT_CLASS SDKCaller
{
public:
EXPORT_CLASS_API SDKCaller(const char* config_path);
public:
// 初始化SDK
EXPORT_CLASS_API int InitSDKBody(const char* model_path_det, const char* model_path_body);
EXPORT_CLASS_API int InitSDKFace(const char* FaceDetect_modelpath, const char* FaceLandmarks_modelpath, const char* MouthLandmarks_modelpath, const char* FaceReconstruct_modelpath);
EXPORT_CLASS_API int InitSDKHand(const char* det_model_path, const char* hand_mesh_model_path, int max_batch);
EXPORT_CLASS_API int InitSDKSMPLX(const char* smplx_path);
// 调用SDK
EXPORT_CLASS_API int CallSDK(const unsigned char* buffer, const int height, const int width, bool start, bool end, int userinfo, char ** outputjson, int &len);
EXPORT_CLASS_API void Destory(char* p);
};
根据我的经验,这个头文件及dll是没毛病的。但是给unity调用的话,这个类SDKCaller怎么映射到Unity中?第二版dll诞生~
2.第二版SDK
在Unity中调用C++写的类,dll直接按上面的方式导出是不行的。我们需要使用函数来代替类,也就是说在上述的类的基础上再封装一层,用函数将类包装起来。直接看源码吧:
(1)修改SDKCaller.h
SDKCaller.h这个头文件要被封装,那它就不对外提供了,所以也不需要加_declspec(dllexport)字段。
#pragma once
#include <string.h>
class SDKCaller
{
public:
SDKCaller(const char* config_path);
public:
int InitSDKBody(const char* model_path_det, const char* model_path_body);
int InitSDKFace(const char* FaceDetect_modelpath, const char* FaceLandmarks_modelpath, const char* MouthLandmarks_modelpath, const char* FaceReconstruct_modelpath);
int InitSDKHand(const char* det_model_path, const char* hand_mesh_model_path, int max_batch);
int InitSDKSMPLX(const char* smplx_path);
int CallSDK(const unsigned char* buffer, const int height, const int width, bool start, bool end, int userinfo, char ** outputjson, int &len);
void Destory(char* p);
};
(2)AvatarDriver.h
AvatarDriver.h对上述的SDKCaller类进行了封装,这个头文件要对外暴露,所以需要加_declspec(dllexport)字段。
#pragma once
#ifdef WIN32
#ifdef DLL_EXPORTS
#define EXPORT_CLASS __declspec(dllexport)
#define EXPORT_API extern "C" __declspec(dllexport)
#define EXPORT_CLASS_API
#else
#define EXPORT_CLASS __declspec(dllimport)
#define EXPORT_API extern "C" __declspec(dllimport)
#define EXPORT_CLASS_API
#endif
#else
#define EXPORT_CLASS
#define EXPORT_API extern "C" __attribute__((visibility("default")))
#define EXPORT_CLASS_API __attribute__((visibility("default")))
#endif
extern "C" {
struct SDKCaller;
EXPORT_CLASS_API SDKCaller* GenerateAISDK(const char* config_path);
EXPORT_CLASS_API int AIInitSDKBody(SDKCaller* sdk,const char* model_path_det, const char* model_path_body);
EXPORT_CLASS_API int AIInitSDKFace(SDKCaller* sdk, const char* FaceDetect_modelpath, const char* FaceLandmarks_modelpath, const char* MouthLandmarks_modelpath, const char* FaceReconstruct_modelpath);
EXPORT_CLASS_API int AIInitSDKHand(SDKCaller* sdk, const char* det_model_path, const char* hand_mesh_model_path, int max_batch);
EXPORT_CLASS_API int AIInitSDKSMPLX(SDKCaller* sdk, const char* smplx_path);
EXPORT_CLASS_API int AICallSDK(SDKCaller* sdk, const unsigned char* buffer, const int height, const int width, bool start, bool end, int userinfo, char** outputjson, int& len);
EXPORT_CLASS_API void AIDestory(SDKCaller* sdk, char* p);
EXPORT_CLASS_API void AIReleaseAISDK(SDKCaller* sdk);
}
(3)AvatarDriver.cpp
AvatarDriver.h只是函数声明,AvatarDriver.cpp才是函数实现,函数内部是对SDKCaller类的成员函数的调用。
#include "SDKCaller.h"
#include "AvatarDriver.h"
SDKCaller* GenerateAISDK(const char* config_path)
{
return new SDKCaller(config_path);
}
inline int AIInitSDKBody(SDKCaller* sdk, const char* model_path_det, const char* model_path_body)
{
return sdk->InitSDKBody(model_path_det, model_path_body);
}
inline int AIInitSDKFace(SDKCaller* sdk, const char* FaceDetect_modelpath, const char* FaceLandmarks_modelpath, const char* MouthLandmarks_modelpath, const char* FaceReconstruct_modelpath)
{
return sdk->InitSDKFace(FaceDetect_modelpath, FaceLandmarks_modelpath, MouthLandmarks_modelpath, FaceReconstruct_modelpath);
}
inline int AIInitSDKHand(SDKCaller* sdk, const char* det_model_path, const char* hand_mesh_model_path, int max_batch)
{
return sdk->InitSDKHand(det_model_path, hand_mesh_model_path, max_batch);
}
inline int AIInitSDKSMPLX(SDKCaller* sdk, const char* smplx_path)
{
return sdk->InitSDKSMPLX(smplx_path);
}
inline int AICallSDK(SDKCaller* sdk, const unsigned char* buffer, const int height, const int width, bool start, bool end, int userinfo, char** outputjson, int& len)
{
return sdk->CallSDK(buffer, height, width, start, end, userinfo, outputjson, len);
}
inline void AIDestory(SDKCaller* sdk, char* p)
{
sdk->Destory(p);
}
inline void AIReleaseAISDK(SDKCaller* sdk)
{
delete sdk;
}
三、Unity调用DLL
我们需要将dll(及其依赖的dll)放入到Unity项目Plugins文件夹下,这个熟悉Unity的人都知道~俺就不细说了。那C++与C#如何映射呢?我们需要用到DllImport命令引入dll(该SDK名称为Avatar_driver.dll),并将AvatarDriver.h以C#语言的方式进行声明。
C++ | C# |
SDKCaller* (自定义类的指针) | IntPtr |
const char* | IntPtr |
const unsigned char* | IntPtr |
作为出参的char** | ref IntPtr |
int& | ref int |
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class AIDLL : MonoBehaviour
{
[DllImport("avatar_driver")]
public static extern IntPtr GenerateAISDK(IntPtr config_path);
[DllImport("avatar_driver")]
public static extern int AIInitSDKBody(IntPtr sdk, IntPtr model_path_det, IntPtr model_path_body);
[DllImport("avatar_driver")]
public static extern int AIInitSDKFace(IntPtr sdk, IntPtr FaceDetect_modelpath, IntPtr FaceLandmarks_modelpath, IntPtr MouthLandmarks_modelpath, IntPtr FaceReconstruct_modelpath);
[DllImport("avatar_driver")]
public static extern int AIInitSDKHand(IntPtr sdk, IntPtr det_model_path, IntPtr hand_mesh_model_path, int max_batch);
[DllImport("avatar_driver")]
public static extern int AIInitSDKSMPLX(IntPtr sdk, IntPtr smplx_path);
[DllImport("avatar_driver")]
public static extern int AICallSDK(IntPtr sdk, IntPtr buffer, int height, int width, bool start, bool end, int userinfo, ref IntPtr outputjson, ref int len);
[DllImport("avatar_driver")]
public static extern void AIDestory(IntPtr sdk, IntPtr p);
[DllImport("avatar_driver")]
public static extern void AIReleaseAISDK(IntPtr sdk);
}
接下来我们就能在unity中进行C++接口的调用咯~
- 字符串转IntPtr,需要用到函数Marshal.StringToHGlobalAnsi()
- IntPtr转字符串,需要用到函数Marshal.PtrToStringAnsi()
IntPtr configPath = Marshal.StringToHGlobalAnsi(Application.streamingAssetsPath + "/config_test.ini");
IntPtr sdk = AIDLL.GenerateAISDK(configPath);
System.IntPtr result = new System.IntPtr();
AIDLL.AICallSDK(GameManager.sdk, pixelPointer, webCamTexture.height, webCamTexture.width, false, false, userInfoSet, ref result, ref len);
string jsonStr = Marshal.PtrToStringAnsi(result);