c# - Unity3D调用外部dll

标签 c# c++ dll visual-studio-2013 unity3d

我正在尝试为 Unity3D Pro (5.0) 构建原生插件。到目前为止,我已经在 VS express 2013 for Windows 中构建了一个 DLL 文件,我已经为此创建了一个示例 Unity 项目并链接了库,但我仍然遇到错误并且我似乎无法移动。谷歌在这件事上不是很有帮助......

我正在尝试为 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;
}

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 文件成功导出,没有任何错误或警告。

统一项目

我将 Dll3.dll 文件放到了 Assets/ 文件夹中。起初,我把它放在 Assets/Plugins/ 中,但我发现它根本不重要。

测试.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 中运行它,作为我桌面上的商店应用程序或将它部署到我的开发平板电脑上。它不在 Unity 编辑器中运行 - 我真是个傻瓜 :-)

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

相关文章:

c# - Xamarin 表格 : ScrollView with KeyBoard

c++ - 在 unordered_map 中分配或插入类对象

c++ - 在全局单例对象中存储 COM 指针的问题

c++ - 如何开始使用 Visual C++ 编写 DLL?

c++ - 使用带有 python 的 DLL(使用 ctypes),不工作

c++ - 如何在 Python 中处理来自 C++ 的 PyObject*

c# - 我如何使用像 C# 这样的 C++ 枚举类型?

c# - 如何在C#和java中实现这种可插拔机制?

c# - 如何在树状结构中列出程序集(包括引用的程序集)中的所有属性?

c++ - 如何以用户友好的形式将 'double' 转换为 'string'