You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2023.3中C++插件调用报EntryPointNotFoundException问题排查

问题分析与解决方案

关键错误点

你遇到的EntryPointNotFoundException主要由以下几个问题导致:

  1. 误用WebGL专用库名
    __Internal是Unity为WebGL平台提供的特殊库名,Windows平台完全不适用,必须替换为你的C++插件实际名称(即Test,对应Test.cpp文件名)。

  2. 调用约定导致名字修饰不匹配
    你在C++中使用了__stdcall调用约定,Windows下编译器会对导出函数名进行修饰,最终导出的名字是_CountLettersInString@4,但你的C#代码中直接查找原函数名CountLettersInString,自然无法匹配。

  3. 插件目录与平台配置错误
    Windows 64位平台对应的Unity插件目录是Assets/Plugins/x86_64,而非x64;且你未对cpp文件设置正确的平台属性,导致Unity没有将其编译为对应平台的插件。


具体解决步骤

步骤1:修正C++代码(二选一)

方案A:修改调用约定避免复杂修饰

将C代码中的__stdcall改为__cdecl,同时保持extern "C"以避免C名字 mangling:

extern "C" __declspec(dllexport) int __cdecl CountLettersInString(wchar_t* str)
{
    int length = 0;
    while (*str++ != L'\0')
        length++;
    return length;
}

方案B:强制指定导出名字

如果必须保留__stdcall,添加链接器指令强制导出原函数名:

extern "C" __declspec(dllexport) int __stdcall CountLettersInString(wchar_t* str)
{
    int length = 0;
    while (*str++ != L'\0')
        length++;
    return length;
}
// 强制导出名为CountLettersInString
#pragma comment(linker, "/EXPORT:CountLettersInString=_CountLettersInString@4")

步骤2:放置文件并设置插件属性

  1. 将Test.cpp移动到Assets/Plugins/x86_64目录(如果没有该目录则手动创建)。
  2. 在Unity编辑器中选中Test.cpp,在Inspector面板中进行如下配置:
    • 展开Platform Settings,找到Windows选项:
      • 勾选Include Platform
      • 将CPU设置为x86_64
    • 取消其他所有平台的Include Platform勾选
    • 确保Plugin Load Settings下的Load on Startup处于勾选状态

步骤3:修正C#脚本的DllImport配置

根据你选择的C++方案对应修改:

对应方案A(__cdecl)

using System.Runtime.InteropServices;
using UnityEngine;

public class CPPTest : MonoBehaviour
{
    // 库名改为Test,指定调用约定为Cdecl,EntryPoint对应修饰后的名字
    [DllImport("Test", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_CountLettersInString")]
    private static extern int CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

    public void DoSomething()
    {
        int i = CountLettersInString("abc");
        Debug.Log(i);
    }
}

对应方案B(强制导出原名字)

using System.Runtime.InteropServices;
using UnityEngine;

public class CPPTest : MonoBehaviour
{
    // 库名改为Test,调用约定为StdCall,直接使用原函数名即可
    [DllImport("Test", CallingConvention = CallingConvention.StdCall)]
    private static extern int CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

    public void DoSomething()
    {
        int i = CountLettersInString("abc");
        Debug.Log(i);
    }
}

步骤4:验证

保存所有文件,重启Unity编辑器,点击按钮调用DoSomething(),此时应该能正常输出字符串长度3。


内容的提问来源于stack exchange,提问作者ABS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:33:32