我是为unity编写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项目来测试它(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));
}
}
当我将脚本放在游戏对象上并点击play时,我得到一个错误,声明为"EntryPointNotFoundException: RandomNumber“,并有一个堆栈跟踪指向Debug.Log()调用。你知道我做错了什么吗?提前谢谢你。
发布于 2021-10-26 15:51:17
您应该指定入口点并使用修饰名称:
将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 -> Tools -> CommandLine -> DeveloperComandPrompt
cd <your PathToDLL>
dumpbin /exports Dll4_CPP.dll
它将打印:
...
1 0 00011217 ?Double@@YAHH@Z = @ILT+530(?Double@@YAHH@Z)
...
https://stackoverflow.com/questions/67525228
复制相似问题