はじめに
今回は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