Nuget Unity



#r 'nuget: Microsoft.Unity.Analyzers, 1.10.1' #r directive can be used in F# Interactive, C# scripting and.NET Interactive. Copy this into the interactive tool or source code of the script to reference the package. Installing Unity.Mvc5 Library from NuGet: In order to install Unity.Mvc5 Library using the NuGet, we need to go to the Package Manager Console and execute the Install-Package Unity.Mvc5 command. To do so, go to the Tools menu and select NuGet Package Manager = Package Manager Console as shown in the below image. Add packages from NuGet to a Unity project. NuGet is the package manager for.NET. NuGet is integrated into Visual Studio. However, Unity projects require a special process to add NuGet packages. This is because when you open a project in Unity, its Visual Studio project files are regenerated, undoing necessary configurations.

Intro

Nuget Unity

Recently I was setting up a new HoloLens 2 project. I usually take the following route:

  • Create a Unity project with the recommended version for the current MRTK (I use 2.4 now, so that is 2018.4.20f1)
  • Switch build platform to UWP
  • Import the MRTK2 as source
  • Add the MRKT2 to the scene
  • Apply optimization settings
  • Select the right sound Spatializer (MS HRTF Spatializer)

And then, since recently, I add NuGet for Unity.

Something is rotten in de state of NuGet

Now this comes a nice UnityPackage that you can download from here. Currently it's at 2.0.1. It works fine in plain Unity projects. It works also fine in UWP build. But if you follow the workflow I show above, it breaks. If you open the NuGet package manager, in stead of the UI you expect, you will see only this:

And you will see these errors in the console:

Nuget for unity
  • System.TypeInitializationException: The type initializer for 'NugetForUnity.NugetHelper' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object
    at NugetForUnity.NugetHelper.LoadNugetConfigFile () [0x0011c] in <b7bde984cef1447da61a3fd28d4789b0>:0
    at NugetForUnity.NugetHelper..cctor () [0x001e6] in <b7bde984cef1447da61a3fd28d4789b0>:0
  • NullReferenceException: Object reference not set to an instance of an object
    NugetForUnity.NugetHelper.LoadNugetConfigFile () (at <b7bde984cef1447da61a3fd28d4789b0>:0)

Great. Now what?

The culprit

When you import the the MRTK2, at some point a NuGet.config file is created. This sits in the Assets root and looks like this:

Nuget Unity3d

For some reason, NuGet for Unity does not get very happy about that. If you quit Unity, delete NuGet.config and the NuGet.config.meta, start again and open the NuGet package manager again, a new NuGet.config is created that looks like this:

Add nuget package

and if you now click select the NuGet package manger again, it starts up normally.

Unity Ioc Nuget

If tried to add the MSBuildForUnity to the package sources like this, because maybe the MRTK2 needs it for something:

but all that does is generate another error:

'Unable to retrieve package list from https://pkgs.dev.azure.com/UnityDeveloperTools/MSBuildForUnity/_packaging/UnityDeveloperTools/nuget/v3/index.jsonSearch()?$filter=IsLatestVersion&$orderby=DownloadCount desc&$skip=0&$top=15&searchTerm='&targetFramework='&includePrerelease=false
System.Net.WebException: The remote server returned an error: (404) Not Found.'

So I actually have no idea why this NuGet.config is created or how. But it definitely needs to go, apparently.

The short version

Nuget Packages In Unity

Before importing NuGet for Unity, check for an existing NuGet.config and NuGet.config.meta and delete those; your NuGet package manager will then work as planned.

Unity Add Newtonsoft

No code today, because all I wrote today here boils down to the one sentence above :)