我正在尝试为Unity3D Pro(5.0)构建本机插件。到目前为止,我已经在Windows的VS Express 2013中构建了一个DLL文件,为此我创建了一个示例Unity项目并链接了该库,但是我仍然遇到错误,而且似乎无法动弹。 Google在这方面不是很有帮助...
我正在尝试为Windows Store目标添加一个包含我自己的低级内容的DLL。
我尝试以这种方式访问​​的内容无关紧要,我被困在hello world示例应用程序中。
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;
}
chh
#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>
文件
#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编辑器中运行-我真是个傻瓜:-)

关于c# - Unity3D调用外部dll,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29258920/

10-09 21:37