簡體   English   中英

Unity3D調用外部dll

[英]Unity3D call external dll

我正在嘗試為Unity3D Pro(5.0)構建本機插件。 到目前為止,我已經在Windows的VS express 2013中構建了一個DLL文件,為此我創建了一個示例Unity項目並鏈接了該庫,但是我仍然遇到錯誤,而且似乎無法動彈。 Google在這方面不是很有幫助...

我正在嘗試為Windows Store目標添加帶有我自己的低級內容的DLL。

我嘗試以這種方式訪問​​的內容無關緊要,我被困在hello world example app中。

Visual Studio項目

Dll3.cpp

#include "pch.h"
#include "Dll3.h"

extern "C" __declspec(dllexport) int testFunc(int a, int b) {
    return a + b;
}

Dll3.h

#pragma once

using namespace std;

extern "C" __declspec(dllexport) int testFunc(int a, int b);

dllmain.cpp

#include "pch.h"

BOOL APIENTRY DllMain(HMODULE /* hModule */, DWORD ul_reason_for_call, LPVOID /* lpReserved */)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
     return TRUE;
}

pch.h

#pragma once

#include "targetver.h"

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif

// Windows Header Files:
#include <windows.h>

pch.cpp

#include "pch.h"

並且我將構建目標設置為x64,該DLL文件已成功導出,沒有任何錯誤或警告。

Unity項目

我將Dll3.dll文件放到Assets/文件夾中。 最初,我在Assets/Plugins/擁有它,但是我發現它根本不重要。

test.cs中

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class test : MonoBehaviour {

    // Dll import according to Unity Manual
    #if UNITY_IPHONE || UNITY_XBOX360
    [DllImport ("__Internal")]
    #else
    [DllImport ("Dll3")]
    #endif
    private static extern int testFunc (int a, int b);

    // Use this for initialization
    void Start () {
        int a = 1;
        int b = 2;
        int c = testFunc (a, b);
        Debug.Log (c.ToString ());
    }

    // Update is called once per frame
    void Update () {

    }
}

然后,我創建了一個空的游戲對象,並為其分配了該腳本,然后進行編譯和運行。 它編譯時沒有任何錯誤或警告,但是運行時(在編輯器中),我得到了:

Failed to load 'Assets/Dll3.dll' with error 'This operation is only valid in the context of an app container.', GetDllDirectory returned ''. If GetDllDirectory returned non empty path, check that you're using SetDirectoryDll correctly.
Failed to load 'Assets/Dll3.dll' with error 'This operation is only valid in the context of an app container.', GetDllDirectory returned ''. If GetDllDirectory returned non empty path, check that you're using SetDirectoryDll correctly.
DllNotFoundException: Assets/Dll3.dll
    test.Start () (at Assets/test.cs:18)

誰能指出我做錯的地方嗎? 我習慣於Unix(OSX / Linux)環境和Windows,這對我來說是非常不標准的。 我不完全了解VS DLL項目的概念。 我將不勝感激。 謝謝

是的,從一開始就是正確的。 我只需要在Visual Studio Simulator中作為桌面上的Store App運行它,或將其部署到我的開發Tablet PC。 它不能在Unity編輯器中運行-我真是個傻瓜:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM