我对编写 Unity 的 C++ 插件非常陌生,但现在必须这样做。我一直在松散地遵循本教程,并在一个名为 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个全新的 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() …
Run Code Online (Sandbox Code Playgroud)