# 程序集定义文件格式参考

> Assembly definition file format reference.

程序集定义和程序集定义引用资源是 JSON 文件。可以使用\_\_检视面板\_\_窗口在 Unity 编辑器中编辑资源文件，不过也可以使用外部工具修改 JSON 内容。

## 程序集定义 JSON##assembly-definition-json

程序集定义是具有以下字段的一种 JSON 对象：

#### allowUnsafeCode bool

可选。默认为 false。请参阅\[允许“不安全的代码”]。

```yml
"allowUnsafeCode" : true
```

#### autoReferenced bool

可选。默认为 true。请参阅\[自动引用]。

```yml
"autoReferenced": false
```

#### defineConstraints string\[]

可选。用作约束的符号。可以为空。请参阅\[定义约束]。

```yml
"defineConstraints": [
        "UNITY_2019",
        "UNITY_INCLUDE_TESTS"
    ]
```

#### excludePlatforms string\[]

可选。要排除的平台名称字符串或空数组。如果 [includePlatforms](#include-platforms) 包含值，则 excludePlatforms 数组必须为空。可以使用 [CompilationPipeline.GetAssemblyDefinitionPlatforms](/engine/6000.0/script-reference/unity-editor-compilation/compilation-pipeline/get-assembly-definition-platforms.md) 函数来获取平台名称字符串（调用此函数时，必须为当前的编辑器安装对平台的支持。）请参阅\[平台]。

```yml
"includePlatforms": [],
"excludePlatforms": [
        "iOS",
        "macOSStandalone",
        "tvOS"
]
```

#### includePlatforms string\[]

可选。要包含的平台名称字符串或空数组。如果 [excludePlatforms](#exclude-platforms) 包含值，则 includePlatforms 数组必须为空。可以使用 [CompilationPipeline.GetAssemblyDefinitionPlatforms](/engine/6000.0/script-reference/unity-editor-compilation/compilation-pipeline/get-assembly-definition-platforms.md) 函数来获取平台名称字符串（调用此函数时，必须为当前的编辑器安装对平台的支持。）请参阅\[平台]。

```yml
"includePlatforms": [
        "Android",
        "LinuxStandalone64",
        "WebGL"
],
"excludePlatforms": []
```

#### name string

必需。任何\[合法程序集名称]。

```yml
"name" : "MyAssemblyName" 
```

#### noEngineReferences bool

可选。默认为 false。请参阅\[无引擎引用]。

```yml
"noEngineReferences": false
```

#### optionalUnityReferences string\[]

可选。在 Unity 的早期版本中，此字段序列化了 **Unity References: Test Assemblies** 选项（该选项用于将程序集指定为测试程序集）。从 Unity 2019.3 开始，不再显示该选项。该字段仍然受支持，但如果在较新版本的 Unity 编辑器中重新序列化资源，则该字段将被等效程序集引用替换。

请参阅\[创建测试程序集]以了解有关测试程序集的更多信息。

```yml
"optionalUnityReferences": [
    "TestAssemblies"
  ]
```

#### overrideReferences bool

可选。如果 [precompiledReferences](#precompiled-references) 包含值，则设置为 true。默认为 false。

请参阅 \[Override References]。

```yml
"overrideReferences": true
```

#### precompiledReferences string\[]

可选。引用的 DLL 库的文件名，包括扩展名，但没有其他路径元素。可以为空。除非将 [overrideReferences](#override-references) 设置为 true，否则会忽略此数组。

请参阅 [Assembly References](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/class-assembly-definition-importer.md#assembly-references)。

```yml
"overrideReferences": true,
"precompiledReferences": [
        "Newtonsoft.Json.dll",
        "nunit.framework.dll"
]
```

#### references string\[]

可选。对使用程序集定义资源创建的其他程序集的引用。可以使用程序集定义资源文件的 GUID 或程序集的名称（由程序集定义的 [name](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/class-assembly-definition-importer.md#name) 字段定义）。必须对列表中的所有引用使用相同格式。可以为空。

可以使用 [AssetDatabase.AssetPathToGUID](/engine/6000.0/script-reference/unity-editor/asset-database/asset-path-to-guid.md) 函数来检索资源的 GUID。（GUID 也是与每个资源关联的元数据的一部分。）

请注意，编辑器在 Assembly Definition 检视面板中显示一个 **Use GUIDs** 选项。此选项不在关联 JSON 文件中进行序列化。而是从文件中找到的引用格式推断选择。

请参阅\[引用另一个程序集]。

使用 GUID：

```yml
"references": [
        "GUID:17b36165d09634a48bf5a0e4bb27f4bd",
        "GUID:b470eee7144904e59a1064b70fa1b086",
        "GUID:2bafac87e7f4b9b418d9448d219b01ab",
        "GUID:27619889b8ba8c24980f49ee34dbb44a",
        "GUID:0acc523941302664db1f4e527237feb3"
]
```

使用程序集名称：

```yml
"references": [
        "Unity.CollabProxy.Editor",
        "AssemblyB",
        "UnityEngine.UI",
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner"
]
```

#### versionDefines object\[]

可选。包含每个版本定义的对象。此对象具有三个字段：

* name:string -- 资源的名称
* expression:string -- 定义资源的版本或版本范围的表达式
* define:string -- 要定义的符号

请参阅 [Version Defines](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/class-assembly-definition-importer.md#version-defines)。

```yml
"versionDefines": [
    {
        "name": "com.unity.ide.vscode",
        "expression": "[1.7,2.4.1]",
        "define": "MY_SYMBOL"
    },
    {
        "name": "com.unity.test-framework",
        "expression": "[2.7.2-preview.8]",
        "define": "TESTS"
    }
]
```

### 示例程序集定义 JSON 字符串##example-with-assembly-names-and-included-platforms

使用对其他程序集定义的引用的程序集名称和 *includePlatforms*：

```yml
{
    "name": "BeeAssembly",
    "references": [
        "Unity.CollabProxy.Editor",
        "AssemblyB",
        "UnityEngine.UI",
        "UnityEngine.TestRunner",
        "UnityEditor.TestRunner"
    ],
    "includePlatforms": [
        "Android",
        "LinuxStandalone64",
        "WebGL"
    ],
    "excludePlatforms": [],
    "overrideReferences": true,
    "precompiledReferences": [
        "Newtonsoft.Json.dll",
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_2019",
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [
        {
            "name": "com.unity.ide.vscode",
            "expression": "[1.7,2.4.1]",
            "define": "MY_SYMBOL"
        },
        {
            "name": "com.unity.test-framework",
            "expression": "[2.7.2-preview.8]",
            "define": "TESTS"
        }
    ],
    "noEngineReferences": false
}
```

使用对其他程序集定义的引用的 GUID 和 *excludePlatforms*：

```yml
{
    "name": "BeeAssembly",
    "references": [
        "GUID:17b36165d09634a48bf5a0e4bb27f4bd",
        "GUID:b470eee7144904e59a1064b70fa1b086",
        "GUID:2bafac87e7f4b9b418d9448d219b01ab",
        "GUID:27619889b8ba8c24980f49ee34dbb44a",
        "GUID:0acc523941302664db1f4e527237feb3"
    ],
    "includePlatforms": [],
    "excludePlatforms": [
        "iOS",
        "macOSStandalone",
        "tvOS"
    ],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "Newtonsoft.Json.dll",
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_2019",
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [
        {
            "name": "com.unity.ide.vscode",
            "expression": "[1.7,2.4.1]",
            "define": "MY_SYMBOL"
        },
        {
            "name": "com.unity.test-framework",
            "expression": "[2.7.2-preview.8]",
            "define": "TESTS"
        }
    ],
    "noEngineReferences": false
}
```

## 程序集定义引用 JSON##assembly-definition-reference-json

程序集定义引用是具有以下字段的一种 JSON 对象：

#### reference string

必需。要引用的程序集定义。请参阅\[程序集定义引用]。

可以使用程序集的名称或资源的 GUID 引用程序集定义资源。可以使用 [AssetDatabase.AssetPathToGUID](/engine/6000.0/script-reference/unity-editor/asset-database/asset-path-to-guid.md) 函数检索资源的 GUID。（GUID 也是与每个资源关联的元数据的一部分。）

使用程序集名称：

```yml
{
    "reference": "AssemblyA"
}
```

使用程序集定义资源 GUID

```yml
{
    "reference": "GUID:f4de40948f4904ecb94b59dd38aab8a1"
}
```

请参阅\[创建程序集定义引用资源]。

{ /*  Start_links  */ }

{ /*  End_links  */ }
