【发布时间】:2021-08-04 02:11:11
【问题描述】:
我对编写统一的 c++ 插件非常陌生,但现在必须这样做。我一直在松散地关注this 教程,并在一个名为 UnityPluginTest 的 Visual Studio dll 项目中创建了以下内容:
#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 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.
-
我见过几个类似的答案,但它们似乎大多与引用或依赖于其他 .dll 文件的 .dll 文件有关。据我所知,在这种情况下并非如此。我正在创建的 dll 没有这样的依赖关系,我尝试将复制的 .dll 文件放在 Assets/Plugins 和统一项目的根目录中,所以我认为这不是 .dll 位置的问题文件已放置。
-
__Internal 仅适用于 IOS,因此请检查一下。
-
我确实尝试使用插件的名称而不是 __Internal,但在这种情况下似乎根本找不到插件。似乎有这个错误,它正在寻找插件,如果我理解正确,只是无法弄清楚在哪里读取正确的功能。
标签: c++ unity3d dll runtime-error