Unity——在C#中调用C++动态链接库(DLL)

一、创建C++动态链接库(DLL)

1、新建C++空项目

打开VS,新建一个C++空项目,自命名项目名称与位置。

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.h,点击添加。

代码如下: 

#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、生成解决方案

点击生成->生成解决方案,生成之后,会显示出生成成功。

7、确定生成.dll文件

在项目路径下x64文件夹的Debug中就可以看到生成的.dll文件

二、将Dll库在Unity工程中调用

1、创建Unity工程

打开Unity创建新项目,添加两个Cube,分别命名为Cube1、Cube2,添加一个空物体命名为Main。

2、将Dll文件放入Unity项目中

在项目下的Assets文件夹下新建一个文件夹命名为Plugins,将之前生成的动态链接库放到Plugins文件夹下

3、新建脚本调用C++代码

新建Main.cs脚本,并把脚本挂载在Main的空物体下。

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class Main : MonoBehaviour
{
    private GameObject cube1;
    private GameObject cube2;
    // Use this for initialization
    void Start()
    {
        cube1 = GameObject.Find("Cube1");
        cube2 = GameObject.Find("Cube2");
        PrintDistanceViaUnity();
    }

    [DllImport("CppAndCS.dll")]
    public static extern float GetDistance(float x1, float y1, float x2, float y2);

    void PrintDistanceViaUnity()
    {
        var pos1 = cube1.transform.position;
        var pos2 = cube2.transform.position;
        Debug.Log("Distance of Two Cube");
        Debug.Log("Distance:" + GetDistance(pos1.x, pos1.y, pos2.x, pos2.y));
    }
}

4、运行结果

控制台输出两个方块的距离,说明成功调用C++的GetDistance方法。

 注:如果Unity已经在运行并且Dll已经存在,那么新的Dll写入生成会失败,此时需要关掉Unity再重新生成。

可能遇到的问题

问题1

问题描述:#include <DllForUnity.h>报错:无法打开源文件。

解决方法:依次点击“项目——配置属性——C/C++——常规”,在“附加包含目录”中加入.h文件所在的文件夹路径。

问题2

问题描述:unity控制台报错: 

 Assets\Main.cs  error CS0246: The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)

Assets\Main.cs   error CS0246: The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)

 解决方法:

1:一定要 using System.Runtime.InteropServices;

2: [DllImport(“XXXX”)] 需要放在类里面使用

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将C++代码打包给Unity调用,可以通过以下步骤: 1. 创建一个C++库项目,并将其编译为DLL文件。 2. 在Unity创建一个C#脚本,使用DllImport特性引用DLL文件的函数。 3. 在C#脚本调用DLL函数。 下面是一个简单的示例: 1. 创建C++库项目,并将其编译为DLL文件。 首先,创建一个C++库项目并实现一些函数。例如,下面是一个简单的Add函数,它将两个整数相加并返回结果: ``` // MyMathLib.h #ifdef MYMATHLIB_EXPORTS #define MYMATHLIB_API __declspec(dllexport) #else #define MYMATHLIB_API __declspec(dllimport) #endif extern "C" MYMATHLIB_API int Add(int a, int b); ``` ``` // MyMathLib.cpp #include "MyMathLib.h" int Add(int a, int b) { return a + b; } ``` 在这里,我们使用了`__declspec(dllexport)`和`__declspec(dllimport)`来指示编译器导出和导入DLL函数。`MYMATHLIB_API`宏用于将函数标记为导出或导入。 2. 在Unity创建一个C#脚本,使用DllImport特性引用DLL文件的函数。 在Unity,我们可以创建一个C#脚本,并使用DllImport特性来引用DLL文件的函数。例如,下面是一个简单的例子: ``` // MyMathLib.cs using System.Runtime.InteropServices; public static class MyMathLib { [DllImport("MyMathLib.dll")] public static extern int Add(int a, int b); } ``` 在这里,我们使用DllImport特性来引用DLL文件的Add函数。`"MyMathLib.dll"`是DLL文件的名称。 3. 在C#脚本调用DLL函数。 现在,我们可以在C#脚本调用DLL函数。例如,下面是一个简单的测试: ``` // Test.cs using UnityEngine; public class Test : MonoBehaviour { void Start() { int result = MyMathLib.Add(2, 3); Debug.Log("Result: " + result); // Output: Result: 5 } } ``` 在这里,我们使用`MyMathLib.Add`来调用DLL文件的Add函数,并将结果打印到Unity控制台。 这就是将C++代码打包给Unity调用的基本步骤。当然,具体实现可能因项目而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值