Unity3d调用C++ dll中的函数

一、生成dll

1.新建dll工程

  1. 不用管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版本

相关推荐
丰色木夕8 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
SmalBox9 小时前
05-03-源码-运行模式体系(PlayMode)
unity3d·游戏开发
衍玊11 小时前
多路转接原理与应用
c++
himobrinehacken12 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
Android小码家15 小时前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
hetao173383715 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法
夏虫不可与之言冰15 小时前
第三课 点亮 LED 与定时器非阻塞闪烁
c语言·c++·vscode·单片机·嵌入式硬件·物联网
辛苦才能15 小时前
C++ 哈希表深度解析:删除为什么必须打标记,负载因子为什么卡 0.7
开发语言·数据结构·c++·算法
miller-tsunami16 小时前
STL简介&string相关知识点
开发语言·c++·string
坤坤子吖16 小时前
Linux 线程同步与互斥——互斥锁
linux·服务器·c++·笔记·学习