マネージバイトコードストリッピングは、マネージアセンブリ (DLL) から使用しないコードを削除します。その処理は、まずルートアセンブリを定義し、それから、静的コード解析を使用してそれらのルートアセンブリが使用するマネージコードを定義します。アクセスできないコードはすべて削除されます。バイトコードストリッピングがコードを難解にすることは なく、また、使用するコードを変更することも ありません。
Unity プレーヤービルドの場合、ルートアセンブリは、スクリプトコード (例えば、Assembly-CSharp.dll) から Unity エディターによってコンパイルされたものです。 スクリプトコードからコンパイルされたアセンブリは削除されませんが、その他のアセンブリ (以下を含む) は削除されます。
マネージバイトコードストリッピングは、 IL2CPP スクリプティングバックエンドが使用されている場合は常に使用可能です。この場合、Stripping Level オプションは Strip Engine Code という名前の Boolean オプションに替わります。このオプションを有効にすると、 ネイティブの Unity エンジンコードで使用されないモジュールとクラスは削除されます。逆に無効にすると、ネイティブの Unity エンジンコードのモジュールとクラスのすべてが保持されます。
link.xml ファイル (後述) を使用すると、型とフルアセンブリを保持することによって効果的にバイトコードのストリッピングを無効にすることができます。例えば、System アセンブリがストリップされるのを防ぐため、以下のような link.xml ファイルを使用します。
<linker>
<assembly fullname="System" preserve="all"/>
</linker>
Stripping depends highly on static code analysis and sometimes this can’t be done effectively, especially when dynamic features like reflection are used. In such cases, it is necessary to give some hints as to which classes shouldn’t be touched.
Unity supports a per-project custom stripping safe list. To create a safe list, create a link.xml
file and put it in the Assets
folder (or any subdirectory of Assets
). An example of the contents of the link.xml
file follows. Classes marked for preservation are not affected by stripping:
<linker>
<assembly fullname="System.Web.Services">
<type fullname="System.Web.Services.Protocols.SoapTypeStubInfo" preserve="all"/>
</assembly>
<assembly fullname="System">
<type fullname="System.Net.Configuration.WebRequestModuleHandler" preserve="all"/>
<type fullname="System.Net.HttpRequestCreator" preserve="all"/>
<type fullname="System.Net.FileWebRequestCreator" preserve="all"/>
</assembly>
<assembly fullname="mscorlib">
<type fullname="System.AppDomain" preserve="fields"/>
<type fullname="System.InvalidOperationException" preserve="fields">
<method signature="System.Void .ctor()"/>
</type>
<type fullname="System.Object" preserve="nothing">
<method name="Finalize"/>
</type>
</assembly>
</linker>
プロジェクトは複数の link.xml ファイルを持つことができます。各 link.xml ファイルでは、多数のオプションを指定することができます。 assembly 要素はマネージアセンブリを示し、ネストされたディレクティブを記述します。
type 要素は、特定の型の処理方法を示すのに使用されます。type 要素は assembly の子要素でなければなりません。fullname 属性には、1 つ以上の文字に該当するワイルドカード ‘*’ を使用できます。
preserve 属性は以下の 3 つ値のうち 1 つを取ります。
method 要素は、指定したメソッドを保持するために使用します。method 要素は type 要素の子でなければなりません。method は name (名前) または signature (署名) で指定します。
link.xml ファイルに加え、ソースコード内で C# の [Preserve]
属性を使って、リンカーがそのコードをストリップするのを避けることができます。この属性は link.xml ファイルに対応するエントリーとは少し違う挙動をします。
[Preserve]
属性があるような挙動)。ストリップされたアセンブリは、プロジェクトの Temp ディレクトリ下の任意のディレクトリに出力されます (正確な場所はターゲットプラットフォームによって異なります)。オリジナルのストリップされていないアセンブリは、ストリップされたアセンブリと同じ場所にある not-stripped ディレクトリで利用可能です。ILSpy のようなツールを使って、ストリップされた、または、されなかったアセンブリを検証し、コードのどの部分が削除されたかを判断することができます。
2017–09–01 修正されたページ
2017–05–26 - Unity 5.6 で Unity ユーザーマニュアルのドキュメントのみを更新
2017–09–01 - Unity 2017.1 で C# [Preserve]
属性の使用に関するヒントを追加
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.