41

I want to use some NuGet packages inside Unity. I achieved that Unity finds the downloaded DLLs according to this article (https://www.what-could-possibly-go-wrong.com/unity-and-nuget/). The nuget.config file can be configured to download the packages into the Plugins folder inside the Assets folder. The Problem is that NuGet downloads multiple versions of each DLL (eg net46, netcore50, netstandard21, so forth) and Unity doesn't like multiple DLLs with the same name. I know I could simply put the DLL inside the Plugins folder by hand, but unfortunately that is not a solution which would please me.

Do you have any idea how I could work around this problem? Is it possible to configure NuGet to just download one DLL for each dependency?

Dharman
  • 30,962
  • 25
  • 85
  • 135
krumbi
  • 511
  • 1
  • 4
  • 4
  • Your article talks about the problem and the approach to solve it. The author uses a separate Visual Studio project to work around this issue. – Cabrra Nov 23 '18 at 13:38
  • 3
    See the MS doc https://learn.microsoft.com/en-us/visualstudio/cross-platform/unity-scripting-upgrade?view=vs-2017#taking-advantage-of-net-compatibility – xmedeko Feb 20 '19 at 11:23

8 Answers8

44

Just thought I'd add this in case it helps anyone

I used the Nuget for Unity asset (free) to import a package (websocketsharp) and it was really easy and painless. The references in VS worked immediately as well

The package you're trying to import naturally has to be compatible with Unity but that's the same even if you import it manually. So I'd recommend giving this a try

Ben
  • 37
  • 1
  • 6
FearlessHyena
  • 3,527
  • 33
  • 39
  • 2
    How do you know if the package is compatible with unity? – Pablo Sep 14 '19 at 19:38
  • 1
    It usually says it's compatible with Unity in the package readme. If it doesn't then the best way to find out is to just try importing it and see Unity reports any compatibility errors. There's some more useful info in [this post](https://stackoverflow.com/questions/43196608/how-do-i-add-unity-support-to-a-nuget-package) – FearlessHyena Sep 17 '19 at 04:57
  • 2
    Package manager throws all kinds of errors with that package. – BrainSlugs83 Jan 29 '21 at 02:17
  • 1
    @BrainSlugs83 Might be a good idea to submit an issue in the repo – FearlessHyena Feb 02 '21 at 10:26
  • 1
    The entirety of the instructions for installing Nuget for Unity consist of: "How do I install NuGetForUnity? Install the provided Unity package into your Unity project." *twitch* – Trevortni Apr 09 '22 at 03:05
33

Here are the details,

1. go to your desired NuGet package webpage.
2. on the right side **Download Package** option click it.
3. your package **.nupkg** file will be downloaded.
4. change its extension to .zip and extract it
5. go to lib and copy your package dll file from net or any netstandard folder. For [your unity project compatibility purposes][2] view this:

enter image description here

    6. open unity workspace and create plugin folder 
    7. paste your dll file here.

Here is the video guide, i have imported newtonsoft.json pacakge in unity

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • But what if you have two .dll files with the same name? one dll is for x64 and the other is for x86 @Muhammad Fauzan Khan – August Jelemson May 29 '20 at 18:54
  • 4
    you should make two folders inside plugin folder x86 and x64 and place your dll files – Muhammad Faizan Khan May 30 '20 at 05:53
  • 5
    for the record, according to this answer you should create `MyUnityProject/Assets/Plugins` and put your `.dll` file in there – Felipe Aug 27 '21 at 19:43
  • 1
    2022 and this is still the best way to do it. Pro-Tip, if you're using 7zip, you can just right-click the nupkg file and unzip to a new folder, skipping the need to rename the extension to .zip – Dave Jellison May 17 '22 at 21:32
  • This isn't working for me. I followed the steps, but my VSCode intellisense won't pick up the class names from the package. Is this just a VSCode quirk or am I actually missing something? – Jason McGinty Dec 03 '22 at 00:07
  • Never mind, I just had to restart the project. It's working now. – Jason McGinty Dec 03 '22 at 00:10
20

You really don't wanna go down the path of configuring Unity to work with Nuget automatically. That article is rather old. With Unity 2018, you get a .net standard 2.0 compatibility level, which should be perfect for Nuget packages. Simply download the package using a separate VS project (as mentioned in the article), then take the netstandard20 version of the DLL and place it in your Unity project.

Arshia001
  • 1,854
  • 14
  • 19
  • Most likely, if the library is implemented with other platforms in mind. JSON.Net (even the dotnet standard version) specifically has problems with AOT platforms, at least inside Unity. – Arshia001 Jun 19 '19 at 07:48
  • 1
    I just used Json.NET in an IL2CPP project and it worked fine. – BrainSlugs83 Jan 29 '21 at 02:18
  • For FluentAssertions use in your EditMode tests: Drop the netstandard20 DLL in the Plugins folder, then open your EditMode Assembly Definition and add the Assembly Reference for FluentAssertions.dll. (some Unity restarts may be required). Then in your test .cs file, add "using FluentAssertions;" and you should be good. – Dustin_00 Mar 27 '21 at 02:17
  • 1
    You save my life. Thank you so much @Arshia001 – Tung Luong Thanh Feb 13 '22 at 11:23
3

Use native Nuget (for packages which targets only one framework)

Instead of downloading everything manually you can create a nuget.config and a packages.nuget file and place them in you project's root directory.

nuget.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
      <!-- This makes Nuget to place the packages at ./Assets/Plugins -->
      <add key="repositoryPath" value="./Assets/Plugins" />
  </config>
  <packageSources>
    <clear /> <!-- Delete packageSources from other nuget.configs -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

packages.nuget:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <!-- targetFramework="netstandard2.0" makes nuget to unzip the netstandard2.0 version of the package-->
  <package id="PackageA" version="1.0.0" targetFramework="netstandard2.0" />
  <package id="PackageADependsOnThis" version="1.0.0" targetFramework="netstandard2.0" />
  <package id="PackageB" version="1.0.0" targetFramework="netstandard2.0" />
</packages>

Usage

To restore these packages open the project's root directory in your terminal and enter the following:

nuget restore -NoCache

You may want to omit -NoCache it prevents nuget from using the cache at C:\Users\<username>\.nuget\packages. This is useful if you use a private nuget feed and there are already packages in your cache which fits your desired packages names and versions but are from a different feed.

The downside of this approach is that you will have both in ./Assets/Plugins the netstandard2.0-Library and the Nuget package like so: Example Files in fileexplorer Nuget packages a usually not that big so that should not be a factor. Also note you this approach will not resolve package dependencies, you will have to list them in packages.config yourself.

Edit: I noticed another downside: NugetPackages which target multiple frameworks, will be installed as well. This leads to an Unity-Error and need to delete these framework-installs yourself.

See also

nuget.config reference

Paul
  • 571
  • 3
  • 17
  • 1
    You can [configure which `.dll` version is used in Unity](https://stackoverflow.com/a/73493205/414413) – Cirdec Aug 25 '22 at 20:10
3

You can use the native dotnet restore or nuget restore commands to populate an asset folder with the nuget packages.

Get the packages

Specify where to put the packages either on the command line or in a nuget.config file so that they will be included as assets in unity.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
      <add key="globalPackagesFolder" value="./NuGetPackages" />
  </config>
</configuration>

This can go in the same folder as your dotnet solution, in which case you can restore the packages with dotnet restore or nuget restore in the solution folder.

Configure which version is used in Unity

In the Unity editor, find the dll files from the package

Unity dll file from a nuget package

Open the file in the inspector and disable it for platforms, or conditionally for build constraints.

Unity dll inspector with all platforms disabled

This gets saved into the .dll.meta files in the same directory as the .dll. You don't want them to be deleted so ...

Configure source control to keep the .dll.meta files

This .gitignore will exclude everything from the packages, but will keep the .dll.meta files that specify which version Unity uses.

**/NuGetPackages/**
!**/NuGetPackages/**/
!**/NuGetPackages/**/*.dll.meta
Cirdec
  • 24,019
  • 2
  • 50
  • 100
2

The process that worked for me was:

  1. Download the NuGet package
  2. Unzip and get the DLL for .NET 4.x
  3. Creating a Plugins folder under Assets (Assets/Plugins)
  4. Pasting the DLL in the Plugins folder

Then Unity threw a bunch of errors in the console, and all of them looked like compatibility issues, so I went back to nuget.org and look at the dependencies of the package. Then I repeated the steps above for each package that unity threw errors for initially.

toxyie_
  • 21
  • 1
0

You have to set up the downloaded nuget /is in packages folder/ plugins manually. Nuget doesn't know which plugin can use unity and how. You can set their parameters in inspector: editor, standalone ... x86,x64 ...

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
0

There is NuGet2Unity that allows to convert any Nuget package to a '.unitypackage'. I've used it to convert "SpecFlow" and i was able to import the resulting Unity Package. Check out their Examples.

It worked for me with:

dotnet.exe run -n specflow --version 3.0.225

Note that you might need to skip dependecies that are deliverd by the NugetPackage which are already deliverd by unity itself.

Roni
  • 11
  • 1