1.打开VS,创建一个C++的空项目,命名为 CppAndCS
2.配置项目属性为动态链接库
右键项目,点击属性,打开项目属性页,将常规中的配置类型修改为动态库(.dll)
3.添加.h头文件
右键头文件,点击 添加->新建项,选择头文件.h,命名为DllForUnity.h,点击添加
代码如下:
#pragma once
#include<math.h>
#include<string.h>
#include<iostream>
#define _DllExport _declspec(dllexport) //使用宏定义缩写下
extern "C"
{
float _DllExport GetDistance(float x1, float y1, float x2, float y2);
}
4.添加.cpp文件
右键源文件,点击 添加->新建项,选择c++文件.cpp,命名为DllForUnity.cpp,点击添加
代码如下:
#include <DllForUnity.h>
float GetDistance(float x1, float y1, float x2, float y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
5.设置编译为c++代码
右键项目,点击属性,对C/C++下面的高级中的编译为选择“编译为C++代码(/TP)”
6.生成解决方案
点击生成->生成解决方案,生成之后,会显示出生成成功。