如果 Unity 项目需要访问默认情况下未编译的 .NET 类库 API 的一部分,则项目可以通知 Unity 中的 C# 编译器。此行为取决于项目使用的 .NET 配置文件。
如果项目使用 .NET Standard 2.0 API 兼容性级别__,应该不需要采取任何其他步骤来使用 .NET 类库 API 的一部分。如果此 API 的一部分似乎丢失,可能是 .NET Standard 2.0 未随附此部分。项目可能需要改用 .NET 4.x API 兼容性级别__。
默认情况下,Unity 在使用 .NET 4.x __API 兼容性级别__时引用以下程序集:
You should reference any other class library assemblies using an csc.rsp file. You can add this file to the Assets directory of a Unity Project, and use it to pass additional command line arguments to the C# compiler. For example, if a Project uses the HttpClient
class, which is defined in the System.Net.Http.dll assembly, the C# compiler might produce this initial error message:
The type `HttpClient` is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
You can resolve this error by adding the following csc.rsp file to the Project:
-r:System.Net.Http.dll
应按照以上示例中所述引用类库程序集。请勿将这些程序集复制到 Project 目录中。
Exercise caution when using an csc.rsp file to reference class library assemblies. If you change the Api Compatibility Level from .NET 4.x to .NET Standard 2.0, and an csc.rsp like the one in the example above exists in the Project, then C# compilation fails. The System.Net.Http.dll assembly does not exist in the .NET Standard 2.0 profile, so the C# compiler is unable to locate it.
The csc.rsp file can have parts that are specific to the current .NET profile. If you make changes to the profile, you need to modify the csc.rsp file.