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版本

相关推荐
.小小陈.12 小时前
C++初阶5:string类使用攻略
开发语言·c++·学习·算法
神奇的代码在哪里12 小时前
C++的演进与我的编程学习之旅:从底层基础到AI应用
c++·人工智能·python·学习·程序人生·个人开发
小年糕是糕手13 小时前
【C++】类和对象(六) -- 友元、内部类、匿名对象、对象拷贝时的编译器优化
开发语言·c++·算法·pdf·github·排序算法
大佬,救命!!!13 小时前
C++本地配置OpenCV
开发语言·c++·opencv·学习笔记·环境配置
噜啦噜啦嘞好13 小时前
Linux:线程池
linux·运维·c++
酷酷的佳13 小时前
用C语言写一个可以排序的程序
c++
SunkingYang13 小时前
如何下载dump(C++程序生成)文件所需要的pdb文件,包含自动下载和手动拼接下载
c++·windbg·dump·dmp·pdb下载·手动下载·拼接下载
..空空的人13 小时前
C++基于protobuf实现仿RabbitMQ消息队列---接口介绍
开发语言·c++·rabbitmq
xlq2232214 小时前
23.二叉树搜索树(下)
数据结构·c++·算法
Source.Liu14 小时前
【学写LibreCAD】Rust Vector2D 实现与 C++ RS_Vector 的对应关系及优势分析
c++·rust·cad