Dll的编写 在unity中加载

时间:2023-03-09 06:49:26
Dll的编写 在unity中加载

1. 在VS中新建Dll项目

2.在头文件中对函数进行声明

extern "C" int _declspec(dllexport) testunity();

3.在源文件中写函数体

int testunity()

{

return 23;

}

4.生成后成功后,在Debug文件中会生成Win32Project1.dll文件

5.在unity中加载,需要把生成的Win32Project1.dll文件,放在项目中的Plugins文件夹下

<pre class="csharp" name="code">using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices; public class DllTest : MonoBehaviour { [DllImport("Win32Project1")]
private static extern int testunity(); void Start()
{
Debug.Log("dll输出:" + testunity());
}
}