Unity - 如何直接添加C++文件以用作插件

问题描述 投票:0回答:1

我使用的是Unity 2023.3。我想在 Unity 中直接使用 C++ 代码作为插件。在他们的文档中,它提到它将 .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 文件:

  1. 资产文件夹
  2. 资产/插件/x64 文件夹
  3. 资源/编辑器文件夹

每次更改 .cpp 文件的位置时,我都会关闭编辑器,将文件移动到文件资源管理器中的新位置,然后重新打开编辑器。

但是,我总是收到此错误: EntryPointNotFoundException:CountLettersInString 程序集:(未知程序集)类型:(未知类型)成员:(null)

我做错了什么?

c# c++ unity-game-engine plugins
1个回答
0
投票

所以我在编辑器中尝试了这个,但我发现编辑器只是Mono,它不支持直接向其中添加C++文件:https://forum.unity.com/threads/include-c-c-source-files -as-plugins-in-il2cpp-back-end.786287/

© www.soinside.com 2019 - 2024. All rights reserved.