一、生成64位的dll
1.用vs新建个工程,随便写个函数
NaviteCode.h
AخA
1
#ifndef __NativeCode_H__
2
#define __NativeCode_H__
3
#ifndef EXPORT_DLL
4
#define EXPORT_DLL __declspec(dllexport) //导出dll声明
5
#endif
6
extern "C" {
7
EXPORT_DLL int MyAddFunc(int _a, int _b);
8
}
9
#endif
NaviteCode.cpp
7
1
#include "NaviteCode.h"
2
extern "C" {
3
int MyAddFunc(int _a, int _b)
4
{
5
return _a + _b;
6
}
7
}
extern “C” 就不用说明了吧,指定c编译器编译,后面打Android的so库也是用相同的代码
2.修改vs导出配置,导出为64位Release的dll(应为用的是unity是64位的)
3.生成,就出来了这个 NativeCode.dll
二、拷贝 NativeCode.dll 到unity工程中
unity存放动态库是由规则的不同平台放置的目录不同。
所以按照规则,把 NativeCode.dll 放入 Assets\Plugins\x86_64 目录中
三、c#中调用一下,随便create个c#挂在场景的对象中
12
1
using UnityEngine;
2
using System.Collections;
3
using System.Runtime.InteropServices; //DllImport需要的namespace
4
public class testDll : MonoBehaviour {
5
[DllImport("NativeCode")] //这里就是调用的dll名字
6
public static extern int MyAddFunc(int x, int y);
7
// Use this for initialization
8
void Start () {
9
int ret = MyAddFunc(200, 200);
10
Debug.LogFormat("--- ret:{0}", ret);
11
}
12
}