如果 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 兼容性级别时引用以下程序集:
应使用 csc.rsp 文件来引用所有其他类库程序集。可将此文件添加到 Unity 项目的 Assets 目录,然后使用该文件将其他命令行参数传递到 C# 编译器。例如,如果项目使用 HttpClient
类(在 System.Net.Http.dll
程序集中定义),C# 编译器可能生成以下初始错误消息:
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'.
要解决此错误,请将以下 csc.rsp 文件添加到项目:
-r:System.Net.Http.dll
应按照以上示例中所述引用类库程序集。必须将这些程序集文件放在项目的 Assets
文件夹内或其中的子文件夹内。
使用 csc.rsp 文件来引用类库程序集时,应该小心。如果将 __API 兼容性级别__从 .NET 4.x 更改为 .NET Standard 2.0,而项目中存在类似于以上示例的 csc.rsp,则 C# 编译会失败。System.Net.Http.dll 程序集并未存在于 .NET Standard 2.0 配置文件中,因此 C# 编译器无法找到该程序集。
csc.rsp 文件可能具有特定于当前 .NET 配置文件的部分。如果更改此配置文件,则需要修改 csc.rsp 文件。
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.