Version: 2022.3
言語: 日本語
Windows 整合性制御
Windows ビルド設定

Windows Player - 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++ コードでまとめてリンクされているため、_P/Invoke への別の DLL はありません。このため、DLL 名の代わりに "__Internal" キーワードを使用します。これにより、関数をランタイムにロードするのではなく、関数を解決するのは 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.

その他の参考資料

Windows 整合性制御
Windows ビルド設定