项目所需,需要在unity中调用C++封装的dll,需要传递结构体数组。C++中封装的函数的参数为结构体指针。在网上找了一下博客,然后结合MSDN上的C#编程参考手册,找到了方法。
DLL的创建
这里创建了一个.h文件,声明了很简单两个结构体和一个类,类中还声明了一个Vector6D的数组,没有使用更复杂的结构体。
//StructTest.h
#ifndef _STRUCTTEST_H_
#define _STRUCTTEST_H_
#include <iostream>
#include <fstream>
using namespace std;
//Vector3D结构体、指向Vector3D的指针、包含Vector3D结构体的结构体:Vector6D
typedef struct
{
int v_X;
int v_Y;
int v_Z;
} Vector3D;
typedef struct
{
Vector3D pos_X;
Vector3D pos_Y;
Vector3D pos_Z;
} Vector6D;
class StructTest
{
public:
Vector3D v3;
Vector6D v6;
Vector6D v6_arr[3];
StructTest();
~StructTest();
void setVector3D(int x, int y, int z);
void setVector6D(Vector3D *v_3); //传入的是Vector3D的指针,其实是一个数组
void setVector6D_Arr(Vector6D *v_6); //这里也一样
bool saveVector(); //这里将数据写入文件,可以比较直观的看到反馈
};
#endif // !_STRUCTTEST_H_
这里是对上面的定义(看着多,其实很简单)
//StructTest.cpp
#include "StructTest.h"
StructTest::StructTest()
{
//初始化v3
v3.v_X = rand() % 6;
v3.v_Y = rand() % 6;
v3.v_Z = rand() % 6;
//初始化v6
Vector3D temp3D;
temp3D.v_X = rand() % 11 + 10;
temp3D.v_Y = rand() % 11 + 10;
temp3D.v_Z = rand() % 11 + 10;
v6.pos_X = temp3D;
temp3D.v_X = rand() % 11 + 10;
temp3D.v_Y = rand() % 11 + 10;
temp3D.v_Z = rand() % 11 + 10;
v6.pos_Y = temp3D;
temp3D.v_X = rand() % 11 + 10;
temp3D.v_Y = rand() % 11 + 10;
temp3D.v_Z = rand() % 11 + 10;
v6.pos_Z = temp3D;
//初始化v6_arr(数组)
for (int i = 0; i < 3; i++)
{
Vector3D temp3D;
Vector6D temp6D;
temp3D.v_X = rand() % 11 + 20;
temp3D.v_Y = rand() % 11 + 20;
temp3D.v_Z = rand() % 11 + 20;
temp6D.pos_X = temp3D;
temp3D.v_X = rand() % 11 + 20;
temp3D.v_Y = rand() % 1