47

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?

2

9 Answers 9

49

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

5
  • 2
    How do you know if the package is compatible with unity?
    – Pablo
    Commented Sep 14, 2019 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 Commented Sep 17, 2019 at 4:57
  • 2
    Package manager throws all kinds of errors with that package. Commented Jan 29, 2021 at 2:17
  • 1
    @BrainSlugs83 Might be a good idea to submit an issue in the repo Commented Feb 2, 2021 at 10:26
  • 2
    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
    Commented Apr 9, 2022 at 3:05
39

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

7
  • 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 Commented May 29, 2020 at 18:54
  • 4
    you should make two folders inside plugin folder x86 and x64 and place your dll files Commented May 30, 2020 at 5:53
  • 5
    for the record, according to this answer you should create MyUnityProject/Assets/Plugins and put your .dll file in there
    – Felipe
    Commented Aug 27, 2021 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 Commented May 17, 2022 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? Commented Dec 3, 2022 at 0:07
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.

4
  • 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
    Commented Jun 19, 2019 at 7:48
  • 1
    I just used Json.NET in an IL2CPP project and it worked fine. Commented Jan 29, 2021 at 2: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
    Commented Mar 27, 2021 at 2:17
  • 1
    You save my life. Thank you so much @Arshia001 Commented Feb 13, 2022 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

1
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
3

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.

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 ...

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.

2
0

shameless plug, but I wrote "BNuget", a tool that integrates Nuget straight into your Unity Assembly Definition Pipeline. It costs some money, but you can check out the docs for it.

https://github.com/cdhanna/BNugetDocs

And its available on the Unity Asset Store. https://assetstore.unity.com/packages/tools/integration/bnuget-269079

How Does BNuget Work?

Unity doesn't use MSBuild for compiling their C# projects. Instead, they use a homegrown tool called Bee. Normally, when you want to reference a Nuget package, you go into your .csproj file, and add a PackageReference field pointing to the Nuget package. Unity doesn't expose .csproj files to the developer at all, mostly because they don't support all the cool stuff you can do in a .csproj file, such as adding Nuget references. Instead, Unity offers Assembly Definitions, which are like .csproj files, but different...

An Assembly Definition defines a list of .cs files to be compiled into a single .dll by their compiler framework, Bee. You can't add Nuget references to an Assembly Definition, but you can add precompiled .dll references... In the Unity Inspector, when you create an Assembly Definition, you can click on it, and add existing .dll files. Those .dll files get included in your project's final build, and you can reference code in those .dll files from within the .cs files included in the Assembly Definition.

BNuget works by adding some extra contextual information to the Assembly Definition, namely, a list of Nuget packages you want, and then works during Unity's Asset Database pipeline to

  1. evaluate all the Nuget dependencies you need,
  2. download the distinct set of required Nuget packages,
  3. extract the .dll files from the packages,
  4. modify the Assembly Definition to reference those recently extracted .dll files.

Now, when Unity goes to compile that Assembly Definition, it will have access to the .dll files included from the Nuget packages, and the .cs source files can use the packages.

That last step, where BNuget modifies the Assembly Definition happens using Unity Asset Post Processors.

Net46 vs NetStandard21 vs other frameworks...

There are a bunch of build targets for Nuget packages, as you listed in your question, like net46, or NetStandard 2.1. A Nuget package can contain prebuild .dll files for many frameworks, which makes them portable to a larger set of consumers. Recent versions of Unity can support NetStandard2.1, which is great. BNuget will automatically extract those NetStandard based .dll files for you out of the package. This means you don't really need to bother with thinking about the various frameworks, but it does mean the Nuget packages you pull in MUST support NetStandard.

How to use BNuget

Here is a video link that walks you through it, https://www.youtube.com/watch?v=YkFo9-x0aFI,

but generally, if you want to access something like Newtonsoft in your C# code, you need to...

  1. Create an Assembly Definition for your src code,
  2. Right click on the Assembly Definition, and select Nuget References,
  3. Click on the newly created .bnuget file,
  4. In the Unity Inspector, you'll see a Nuget package explorer, so search for "Newtonsoft", and click "Install" on the package
  5. Go back to your C# code, and you should have the ability to reference Newtonsoft.
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.