Unity3d 調用C++寫的DLL
- 一、建立DLL
- 打開VS2010,建立一個win32應用程序,選擇建立一個DLL類型的空項目。
- 新建一個頭文件和一個源文件。
- 在頭文件中寫入
- #if defined (EXPORTBUILD)
- # define _DLLExport __declspec (dllexport)
- # else
- # define _DLLExport __declspec (dllimport)
- #endif
-
- extern "C" int _DLLExport MyADD(int x,int y);
- 在源文件中定義方法的操做
- #define EXPORTBUILD
-
- #include "DLL.h"
-
- int _DLLExport MyADD(int x,int y)
- {
- return x+y;
- }
- 傳入兩個參數會返回兩個參數的和,而後編譯這個項目,將生成的dll拷貝到Unity工程中的Asset/Plugins文件夾中
- 二、調用DLL
- 使用C#來調用DLL,首先建立一個C#腳本。添加using指令
- using System.Runtime.InteropServices;
- 使用[DllImport("Dll名字")]指明要引用的DLL,而後聲明要使用的DLL中的方法。
-
- using UnityEngine;
- using System.Collections;
- using System.Runtime.InteropServices;
-
- public class test : MonoBehaviour {
- [DllImport("test")]
- private static extern int MyADD(int x,int y);
- int i = MyADD(5,7);
-
- void OnGUI()
- {
- GUI.Button(new Rect(1,1,200,100),i.ToString());
- }
- }
歡迎關注本站公眾號,獲取更多信息