MrMazerman
MrMazerman

Reputation: 41

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

Upvotes: 2

Views: 1716

Answers (2)

Bozhidar Stoyneff
Bozhidar Stoyneff

Reputation: 3634

This issue happens when you use some of the new features in C# 7 or C# 8. In my case, I was working on an older project, built on .NET framework 4.6 and switched some using blocks to simplified using statements. E.g., instead of

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

I did:

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

The compiler silently allowed me to do this but the next build failed with the CS0006. It took me an hour to figure that out. I was about to re-install my Windows...LOL

Upvotes: 0

aybe
aybe

Reputation: 16662

The behavior you are getting is expected.

Let's go through the official way to achieve what you're looking for:

  1. Copy your DLL in Assets directory, optionally, with its associated XML for IntelliSense, and the PDB file so that Unity generates an MDB file out of it
  2. Unity rebuilds the project and the generated Visual Studio solution

Since step 2 re-generates the Visual Studio solution, every customization you've done to it are lost, exceptions for manually added existing projects for being able to step through them when debugging your game.

Note that nowhere in the official instructions you are expected to add references to the generated projects, it's done automatically and manual changes are lost anyway as laid out in previous paragraph.

But you are getting errors, right ?

It's hard to tell without more information.

Suggestions:

1.

Try the official instructions and if it doesn't work, edit your question to add the relevant errors so people can further try to help you.

2.

Do your DLL matches the framework version of Unity ? In some cases it can produce funny errors otherwise.

When I look at the contents of System.IO.Compression.ZipFile, it's only for .NET 4.6:

enter image description here

Do your player settings match .NET 4.6 ?

enter image description here

Upvotes: 0

Related Questions