繁体   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