extern "C" int _declspec(dllexport)testunity(); extern "C" int _declspec(dllexport)addT(int a,int b); int testunity() { return 3;/*这是函数,里面可以写你想要实现的任何功能 */} int addT(int a,int b){return a*2+b*3;}
set PATH=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE; set INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\; set LIB=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\;C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\; cl /c Test.cpp link /dll Test.obj pause
然后编译、组建。就生成了testunity.dll文件。然后再把这个文件放在你的unity工程的assert的Plugins(如果没有这个文件,那你就要新建)。
然后在unity里面新建C#脚本文件dlltest。代码如下
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class main : MonoBehaviour { [DllImport("uDlltest")] private static extern int testunity(); [DllImport("uDlltest")] private static extern int addT(int a,int b); // Use this for initialization void Start () { print(testunity().ToString() + "," + addT(2,3).ToString()); } // Update is called once per frame void Update () { } }