首页 > 解决方案 > Unity: Visual Studio loses DLL reference after Unity compiles code

问题描述

I am using the System.Compression.ZipFile.dll in a Unity project, and have added the reference to both the the Visual Studio project and within the Unity Editor, and the code compiles and runs as expected without issue.

However, whenever Unity compiles the code, the DLL reference is removed from the Visual Studio Project.

As a result, compiling within Visual Studio afterwords will have namespace errors. These errors are only in in Visual Studio, as the Unity editor does have the reference when it actually compiles the code.

These errors are fixed by re-adding the DLL reference to the solution. I would like to stop the errors from showing up in Visual Studio, as they are rather annoying.

How do I prevent the reference from being removed from the Visual Studio Project when Unity actually compiles the code?

If it helps I am using Visual Studio Community 2015, and the Unity Editor 2018.2.14f1, on Windows 8.1, with .NET version 4.7.1

EDIT:

My mcs.rsp file contains: -r:System.IO.Compression.FileSystem.dll based on a unity forum thread. This is interesting as it is not the expected DLL, and yet it still works.

Adding -r:System.IO.Compression.ZipFile.dll will cause: error CS0006: Metadata file `System.IO.Compression.ZipFile.dll' could not be found.

The relevant errors are:

CS1069 The type name 'ZipFile' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, Consider adding a reference to that assembly.

Error CS0006 Metadata file ProjectFolder\Temp\bin\Debug\Assembly-CSharp.dll' could not be found

Unity is correctly configured to use .NET 4.x

标签: c#visual-studiounity3d

解决方案


当您使用 C# 7 或 C# 8 中的一些新功能时,会发生此问题。在我的例子中,我正在处理一个基于 .NET 框架 4.6 的旧项目,并将一些 using 块切换为简化的 using 语句。例如,而不是

using (var resource = new ExpensiveStuff())
{
   //whatever
}

我做了:

using var resource = new ExpensiveStuff();
//whatever

编译器默默地允许我这样做,但下一次构建失败,CS0006。我花了一个小时才弄明白。我正要重新安装我的 Windows ......哈哈


推荐阅读