網(wǎng)上已經(jīng)有很多類似的了。這里作為自己的筆記,記錄如何做一個(gè)簡(jiǎn)單DLL(Dynamic Link Library)動(dòng)態(tài)鏈接庫(kù)。
創(chuàng)建使用環(huán)境為VS2015。
using UnityEngine;using System.Runtime.InteropServices; //[DllImport]特性的命名空間。public class TestScript : MonoBehaviour{ public int x; public int y;#if UNITY_STANDALONE_WIN //WINDOWS系統(tǒng) const string dll = "MyMaxNumberDLL";#elif UNITY_STANDALONE_OSX //OSX(Mac)系統(tǒng) const string dll = "MyMaxNumberBUNDLE";#elif UNITY_IOS //IOS系統(tǒng) const string dll = "__Internal";#else //Android或其他 const string dll = " ";#endif [DllImport(dll)] private static extern int GetMaxNumber(int x, int y); public static int GetMax(int x,int y) { #if (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS) return GetMaxNumber(x, y); #else return -1; #endif } private void Start() { Debug.Log(GetMax(x, y)); }}
在使用C++的DLL時(shí),出現(xiàn)了一個(gè)錯(cuò)誤:“Failed to load ‘Assets/Plugins/MyMaxNumberDLL.dll’, expected x64 architecture, but was x86 architecture. You must recompile your plugin for x64 architecture.”在stackoverflow有人回答說需要多導(dǎo)入除了.dll文件的另外4個(gè)文件,但無果,又設(shè)置了Unity中Build Settings中windows平臺(tái)的Architecture,但也無果。最后發(fā)現(xiàn)原來是C++ DLL編譯生成時(shí)選擇了x86,改成x64重新編譯導(dǎo)入就好了。
需要選擇x64架構(gòu)。
聯(lián)系客服