Version: 2022.3
Windows integrity control
Windows Build Settings

Windows 播放器:IL2CPP 脚本后端

You can use IL2CPP as an alternative to Mono for scripting backend when building projects for Windows Player.

使用 IL2CPP 构建项目时,Unity 会在创建本机二进制文件之前将脚本和程序集内的 IL 代码转换为 C++。请参阅 IL2CPP 以了解更多信息。

C++ source code plugins for IL2CPP

You can add C++ (.cpp) code files directly into a Unity Project when using the IL2CPP scripting backend. The C++ files act as plugins within the Plugin Inspector. If you configure the C++ files to be compatible with Windows Player, Unity compiles them together with C++ code that gets generated from managed assemblies.

To view the plugin importer settings for C++ files, click a .cpp file and select the appropriate Windows option in the Platform settings section of the Inspector:

关于 C++ 文件的插件导入器设置
关于 C++ 文件的插件导入器设置

因为函数与生成的 C++ 代码链接在一起,所以没有单独的 DLL 可进行 _P/Invoke 调用。因此,可使用"__Internal"关键字代替 DLL 名称,从而使 C++ 链接器负责解析函数,而不是在运行时加载函数,如下例所示:

[DllImport("__Internal")]
private static extern int
CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

You can define this kind of function in NativeFunctions.cpp as follows:

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

Because the linker resolves the function call, any error made in the function declaration on the managed side (C# code that executes under managed run time) produces a linker error instead of a run-time error. This means, no dynamic loading can take place during run time, and the function is called directly from C#, which significantly decreases the performance overhead of a P/Invoke call.

Unity compiles source code plug-ins with the same C++ compiler arguments as the generated C++ code, which can’t be modified. If some plug-in source code requires control over C++ compiler arguments, you must build a native plug-in instead. For more information, see Native plug-in.

IL2CPP build files

A project using the IL2CPP scripting backend typically produces these files:

构建期间生成的 IL2CPP 文件
构建期间生成的 IL2CPP 文件

The following files are common to projects that use IL2CPP:

文件: 描述:
a_Data 包含游戏数据的文件夹。
a.exe 主游戏可执行文件。
UnityCrashHandler64.exe 崩溃处理程序可执行文件。
UnityPlayer.dll 包含所有本机代码的 Unity Player 库。
WinPixEventRuntime.dll PIX for Windows runtime. This file is present only in development builds.
a_BackUpThisFolder_ButDontShipItWithYourGame 包含调试游戏所需数据的文件夹,包括 PDB(调试信息)文件和脚本生成的 C++ 代码。应为发布的每个构建备份此文件夹,但不要重新分发它。
GameAssembly.dll 包含 IL2CPP 运行时和所有脚本代码的库。
SymbolMap File containing a list of all managed function addresses and their lengths. IL2CPP needs this to resolve managed stack traces. If you delete it, you can still run your game but there’s no gurantee that exceptions will generate sensible call stacks.

Additional resources:

Windows integrity control
Windows Build Settings