NuGet packages in Unity
Solution 1
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
Solution 2
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:
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
Solution 3
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.
Solution 4
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:
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
Related videos on Youtube
krumbi
Updated on July 10, 2022Comments
-
krumbi almost 2 years
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?
-
Cabrra over 5 yearsYour article talks about the problem and the approach to solve it. The author uses a separate Visual Studio project to work around this issue.
-
xmedeko about 5 yearsSee the MS doc docs.microsoft.com/en-us/visualstudio/cross-platform/…
-
-
Arshia001 almost 5 yearsMost 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.
-
Pablo over 4 yearsHow do you know if the package is compatible with unity?
-
FearlessHyena over 4 yearsIt 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
-
August Jelemson almost 4 yearsBut 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
-
Muhammad Faizan Khan almost 4 yearsyou should make two folders inside plugin folder x86 and x64 and place your dll files
-
NOT_A_ROBOT over 3 yearsThe NuGet2Unity link is broken.
-
BrainSlugs83 over 3 yearsPackage manager throws all kinds of errors with that package.
-
BrainSlugs83 over 3 yearsI just used Json.NET in an IL2CPP project and it worked fine.
-
FearlessHyena about 3 years@BrainSlugs83 Might be a good idea to submit an issue in the repo
-
Dustin_00 about 3 yearsFor 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.
-
Felipe over 2 yearsfor the record, according to this answer you should create
MyUnityProject/Assets/Plugins
and put your.dll
file in there -
Tung Luong Thanh about 2 yearsYou save my life. Thank you so much @Arshia001
-
Trevortni about 2 yearsThe 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
-
Dave Jellison almost 2 years2022 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