我正在使用 Unity 2023.3。我想直接将C++代码用作Unity中的插件。在他们的文档中,它提到它将.cpp检测为插件:https://docs.unity3d.com/2023.1/Documentation/Manual/PluginInspector.html
我按照此处的说明在 Unity 中使用 .cpp 文件:https://docs.unity3d.com/2021.2/Documentation/Manual/WindowsPlayerCPlusPlusSourceCodePluginsForIL2CPP.html
我使用的是 Windows PC。
我的Test.cpp文件是:
extern "C" __declspec(dllexport) int __stdcall CountLettersInString(wchar_t* str)
{
int length = 0;
while (*str++ != L'\0')
length++;
return length;
}
我在 Unity 中创建的 C# 脚本是:
using System.Runtime.InteropServices;
using UnityEngine;
public class CPPTest : MonoBehaviour
{
[DllImport("__Internal")]
private static extern int
CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);
public void DoSomething()
{
int i = CountLettersInString("abc");
Debug.Log(i);
}
}
我通过按下按钮来调用 DoSomething()。
我已将播放器设置 -> 配置 ->脚本后端设置为 IL2CPP for Windows。
我尝试在以下位置添加.cpp文件:
- Assets 文件夹
- Assets/Plugins/x64 文件夹
- Assets/Editor 文件夹
每次更改.cpp文件的位置时,我都会关闭编辑器,将文件移动到文件资源管理器中的新位置,然后重新打开编辑器。
但是,我总是收到此错误: EntryPointNotFoundException: CountLettersInString assembly:(未知程序集) type:(未知类型) member:(null)
我做错了什么?