Resolving build issues for UWP packages with Unity

Sept. 1, 2017
protect

Image result for visual studio tools for unity

View source image

If you are building games and projects in Unity and targeting the Universal Windows Platform (UWP), you may have noticed than in the recent Unity releases this was actually broken. What you end up seeing in your build UWP project in visual studio is the following error:

The command “”C:\ <Project>\Export\UWP\Unity\Tools\SerializationWeaver\SerializationWeaver.exe” “C:\ <Project>\Export\UWP\GeneratedProjects\UWP\Assembly-CSharp-firstpass\bin\x64\Debug\Unprocessed\Assembly-CSharp-firstpass.dll” “-pdb” “-verbose” “-unity-engine=C:\ <Project>\Export\UWP\Lonely Shadows\Unprocessed\UnityEngine.dll” “C:\ <Project>\Export\UWP\GeneratedProjects\UWP\Assembly-CSharp-firstpass\obj\x64\Debug\x64\Debug” “-lock=C:\ <Project>\Export\UWP\GeneratedProjects\UWP\Assembly-CSharp-firstpass\project.lock.json” “@C:\ <Project>\Export\UWP\GeneratedProjects\UWP\Assembly-CSharp-firstpass\SerializationWeaverArgs.txt” “-additionalAssemblyPath=C:\ <Project>\Export\UWP\Lonely Shadows\Unprocessed” “-unity-networking=C:\ <Project>\Export\UWP\Lonely Shadows\Unprocessed\UnityEngine.Networking.dll”” exited with code 1.
Assembly-CSharp-firstpass          C:\ <Project>\Export\UWP\GeneratedProjects\UWP\Assembly-CSharp-firstpass\Assembly-CSharp-firstpass.csproj   

If you dig further, you may also expose the underlying error code here:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at UnityEditor.Scripting.Compilers.NuGetPackageResolver.Resolve()
   at Unity.NuGetAssemblyResolver..ctor(String projectLockFile)
   at usw.Weaver.ReaderParameters(String assemblyPath, ConversionOptions options)
   at usw.Weaver.Weave()
   at usw.Program.RunProgram(ConversionOptions options)
   at usw.Program.Main(String[] args)

This prevents you building / updating projects from Unity to the UWP Platform.

Once discovered, both Unity and Microsoft worked closely together to resolve the issues and updated their respective parts as quickly as possible.

Fixing the problem

The fix for this problem is fairly easy. However, it is a little time consuming (unless you have a mega fast download connection) as mostly it requires updating to the latest Unity and Visual Studio patch releases.

1: Install Unity 2017.1p5 or 2017.2p9

Image result for unity logo

View source image

The first step, is to simply update your installation of Unity:

  • If you are on Unity 2017, then this is 2017.1p5 (or newer)

  • If you are on Unity 2017.2, then this is 2017.2p9 (or newer)

  • For the 5.x cycle, just update to the latest patch (although I haven’t tested this)

This will update both Unity and your install of the Visual Studio Tools for Unity

2: Update VS 2017 to 15.3.3

Once Unity is up to date, you will need to update your installation of Visual Studio.  If you are still on VS 2015, then there is no action but you won’t be able to build UWP packages targeting the newer Creators update and won’t be able to build Mixed Reality platform.

I highly recommend updating to 2017, even the free Community edition of 2017 (which is free) if you are doing UWP development.  It’s just better.

To update Visual Studio 2017, simple close all open instances of Visual Studio and launch the Visual Studio Installer

image

image

Once it’s running (the first step may be to update the installer first), simply hit “Update” on your specific instance of Visual Studio (the installer will happily update ALL installed instances if you wish) and once it’s complete you will be on the latest version.

Open Visual Studio to verify, click “Help –> About Visual Studio” in the menu and you should be running 15.3.3 (or newer). If not, check your internet connection and try running the installer again.

image

image

3: (Optional) Set player settings for project to .NET & .NET4.6 (NOT IL2CPP, not tested but reports say it doesn’t play nice with Live)

Not strictly required but highly recommended for UWP projects, is to update the .NET Api Compatibility Level that is used in your built project.  This allows you to use more modern C# 6 functionality if you wish without causing errors when you build it in Unity.

To update this, open the Player Settings window in the editor using either “Edit –> Project Settings –> Player” in the editor menu, or using the “Player Settings” button on the “Build Settings” build window.  In the Other section on this configuration page you will find the following settings.

image

image

Not to be confused with the “Scripting Runtime Version”, which I’m told breaks UWP project builds (although I haven’t personally tested yet)

4: Build project targeting UWP SDK 14393 or higher

To build for Modern UWP, you need to be targeting a minimum API level of 14393 (Anniversary Edition), For Mixed Reality builds you will need a minimum API level of 15063 (Creators Update).  Either will work but you need to ensure you select the version that is right for your target.  The current advice with new builds is to always target the latest but that is completely up to you.

You will find the SDK selection on the “Build Settings” screen when you have the “Universal Windows Platform” target selected:

image

image

5: Open project in VS

Once you have built your project, open it in Visual Studio to continue.

6: UPDATE NETCore NuGet package to 5.4+ <- without this, it still doesn’t work

In testing I have found this is critical still for existing projects or when

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>