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
- evaluate all the Nuget dependencies you need,
- download the distinct set of required Nuget packages,
- extract the
.dll
files from the packages,
- 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...
- Create an Assembly Definition for your src code,
- Right click on the Assembly Definition, and select Nuget References,
- Click on the newly created
.bnuget
file,
- In the Unity Inspector, you'll see a Nuget package explorer, so search for "Newtonsoft", and click "Install" on the package
- Go back to your C# code, and you should have the ability to reference Newtonsoft.