EntryPointNotFoundException 与 Unity 插件。

5

我在编写Unity的C++插件方面非常新,并且必须现在这样做。我一直在松散地遵循这个教程,并在Visual Studio DLL项目中创建了一个不太有创意的名为UnityPluginTest的东西:

#include <stdint.h>
#include <stdlib.h>
#include <time.h>

#define DLLExport __declspec (dllexport)

extern "C"
{
    DLLExport int RandomNumber(int min, int max)
    {
        srand((unsigned int)time(0));
        return (rand() % (max - min) + min);
    }
}

我创建了一个全新的Unity项目来测试它(如果有影响,Unity版本为2020.2.f1),并将编译后的.dll文件复制到新文件夹Assets/Plugins中。然后我创建了一个名为TestFirstUnityPluginTest.cs的新脚本(同样是无创意的),其中包含以下内容:

using System.Runtime.InteropServices;
using UnityEngine;

public class TestFirstUnityPluginTest : MonoBehaviour
{
    const string dll = "__Internal";

    [DllImport(dll)]
    private static extern int RandomNumber(int min, int max);

    void Start()
    {
        Debug.Log(RandomNumber(0, 10));
    }
}

我将脚本放到游戏对象上并点击播放,出现错误提示"EntryPointNotFoundException: RandomNumber",堆栈跟踪指向 Debug.Log() 调用。有任何想法是我做错了什么吗?先谢谢您。


这个链接可能会解决你的问题:link - Walter
我看到了几个类似的答案,但它们大多似乎与引用或依赖其他.dll文件有关。就我所知,在这种情况下并非如此。我正在创建的dll没有这样的依赖关系,并且我已经尝试将复制的.dll文件放在Assets/Plugins和Unity项目的根目录中,因此我不认为这是.dll文件放置位置的问题。 - HommusVampire
Internal仅适用于IOS,请注意查看。 - aybe
我尝试使用插件的名称而不是__Internal,但在那种情况下似乎根本找不到插件。看起来在这个错误中,它找到了插件,只是无法找到正确的函数读取位置,如果我理解正确的话。 - HommusVampire
1个回答

1

您应该指定入口点并使用DECORATED名称:

将[DllImport(dll)]替换为[DllImport("YOUR_DLL_NAME.dll", EntryPoint = "DecoratedFunctionName")]

我的C++代码:

__declspec(dllexport) int Double(int number)
{
    return number * 2;
}

我的Unity3d C#代码:

[DllImport("Dll4_CPP.dll", EntryPoint = "?Double@@YAHH@Z")]
public static extern int Double(int number);
void Start()
{
    Debug.Log(Double(10));
}

装饰名称 - DLL 内部函数的名称(编译器会对其进行重命名)。 Dumpbin.exe 可以帮助找到它: VisualStudion2019 -> 工具 -> 命令行 -> 开发人员命令提示符

cd <your PathToDLL>
dumpbin /exports Dll4_CPP.dll

它将打印:

...
1    0 00011217 ?Double@@YAHH@Z = @ILT+530(?Double@@YAHH@Z)
...

源代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接