はじめに
今回はasmdef
で必須パッケージと任意パッケージを定義する方法について紹介をしたいと思います。


その前に
AssemblyDefinitionFiles
ってなんだっけと言う方は一度公式ドキュメント等を参照してみてください。
また今回利用する知識は以下の3つ。
references
:アセンブリの参照設定versionDefines
:特定のパッケージ(かつ特定バージョン)がある場合にのみシンボル定義を行うdefineConstraints
:特定のシンボルが定義されているときのみアセンブリを含める
やり方
必須パッケージのやり方
references
とdefineConstraints
とversionDefines
を利用します。
{ "name": "Sample1", "rootNamespace": "", "references": [ "Unity.Burst" ], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [ "ENABLE_BURST" ], "versionDefines": [ { "name": "com.unity.burst", "expression": "", "define": "ENABLE_BURST" } ], "noEngineReferences": false }

この場合はcom.unity.burst
がなければ、asmdef
で定義しているアセンブリはコンパイルに含まれないようになります。
任意パッケージのやり方
references
とversionDefines
を利用します。
{ "name": "Sample1", "rootNamespace": "", "references": [ "Unity.Burst" ], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], "versionDefines": [ { "name": "com.unity.burst", "expression": "", "define": "ENABLE_BURST" } ], "noEngineReferences": false }
ここで重要なことはreferences
に含まれているアセンブリ(パッケージ)がなくてもエラーにはならないということですね。

またコードを書く上で、任意パッケージの利用箇所は#if ~ #endif
を用いてコンパイルエラーにならないように書いてあげてください。
// 雑サンプル #if ENABLE_BURST using Unity.Burst; using UnityEngine; [BurstCompile] public sealed class BurstDirectCallTest : MonoBehaviour { // 省略 /// <summary> /// 32bitのXorShift /// </summary> [BurstCompile] private static uint XorShift(uint value) { value ^= value << 13; value ^= value >> 17; value ^= value << 5; return value; } } #endif