Unity3d调用C++ dll中的函数

一、生成dll

1.新建dll工程 

2. 不用管dllmain.cpp,添加自定义Helper.h和Helper.cpp

3.添加要在外部调用的方法

//头文件
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int _stdcall Addition(int x, int y);
DLLEXPORT int _stdcall create_video_decoder(int codec_id, void **handle);
//源文件
DLLEXPORT int _stdcall Addition(int x, int y)
{
	return x + y;
}

DLLEXPORT int _stdcall create_video_decoder(int codec_id, void **handle)
{
    //...
}

4.配置x64或x86,编译,得到dll

 二、Unity3d中调用

1.在Unity3d工程Assets建立Plugins文件夹,将dll及依赖dll都复制到该文件夹下

2.在代码中引入函数

注意:C#函数的名字可以与C++中的函数可以不一样。但EntryPoint中的名字必须与C++函数名一样。

[DllImport("MyFFmpegHelper")]
public static extern int Addition(int x, int y);

[DllImport("MyFFmpegHelper", EntryPoint = "create_video_decoder", CallingConvention = CallingConvention.Cdecl)]
private static extern int CreateVideoDecoder(FFmpegVideoCodecId videoCodecId, out IntPtr handle);

3.设置x64或x86以及.net版本

相关推荐

  1. 【名词解释】Unity3D坐标系

    2024-03-10 22:32:07       27 阅读
  2. 【名词解释】Unity3D“刚体移动”

    2024-03-10 22:32:07       35 阅读
  3. Unity3D如何降低游戏Drawcall详解

    2024-03-10 22:32:07       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 22:32:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 22:32:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 22:32:07       82 阅读
  4. Python语言-面向对象

    2024-03-10 22:32:07       91 阅读

热门阅读

  1. GPT的磁盘管理

    2024-03-10 22:32:07       45 阅读
  2. volatile

    2024-03-10 22:32:07       43 阅读
  3. HTML5- 拖拽功能

    2024-03-10 22:32:07       37 阅读
  4. Flask基于配置文件添加项目config配置

    2024-03-10 22:32:07       39 阅读
  5. 深入了解 Python 的 compile() 函数

    2024-03-10 22:32:07       42 阅读
  6. python中def一个方法,就一定对应一个return吗

    2024-03-10 22:32:07       40 阅读
  7. AI辅助研发

    2024-03-10 22:32:07       45 阅读
  8. 2022护网面试题总结

    2024-03-10 22:32:07       38 阅读