Using GCP NuGet Packages with Unity

Jon Foust
4 min readAug 5, 2019

--

I’ve been working with some Google APIs in Unity, and while it has been fun, it was not an easy task getting them to work. I’ve decided to pass along my steps to working with Google APIs and Unity in part to answer questions/requests I frequently received at game conferences. In writing this post, I assume you have already created created a project in Unity and are just looking to integrate Google APIs. If not, there are many tutorials and courses online on getting started with Unity and creating your first project.

Importing NuGet Packages

To get started with Google APIs in Unity, you need to open up a C# script from Unity for editing.

Creating a Scripts Folder and Creating a C# Script

Open or create a C# script within Unity to launch the project solution in Visual Studio(you may use another editor but this process should be the same).

Opening the Script in Visual Studio

Once your script opens, the next step is to locate where to add NuGet packages to your solution. The locations are different on Windows and macOS(Project > Add NuGet Packages > Select desired package > Add Package):

Opening NuGet Package Manager in Visual Studio for Windows

Using the package manager is very simple, but if you feel more inclined to use the package manager console feel free to. Once the package manager is open, click the Browse tab and type in “Google”. When the search results have populated, select an API (make sure it is a “recommended version client library”) and click Install.

Installing the NuGet Package

This will install all libraries necessary to run the chosen NuGet package. Close the manager and save your project. Next, switch over to Unity to prepare for the next step.

Copying Library Files to Unity’s Asset Folder

You’ll notice upon returning to Unity an error message(s) in the console window(you may have to go the Console tab in the Unity editor).

Error in Console Window in Unity

According to Microsoft’s documentation, “ 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”. This causes many problems for us since NuGet packages are usually downloaded and pretty much turnkey. To use the downloaded library file, create a folder in your Assets folder named Plugins.

Creating a Plugins Folder

Next, following the error list in Unity, navigate to the Packages folder in your Unity project (this folder will have all the NuGet packages you’ve downloaded). Once you have located the library you intend to use, copy the file into your newly created Plugins folder.

Copying a Library to the Plugins Folder

The next step is to create the link.xml file that will preserve all library data when migrated to Unity’s platform. Unity’s platform (written in C++) creates binaries for the platform of your choice, so to make sure our libraries do not lose any information, this step is necessary. Make sure to save this file in your project’s root folder. Within the link.xml file, you want to have something similar to the following to ensure that you retain the libraries’ full contents:

Creating the link.xml File and Adding Library Information for Unity Bytecode Stripping

The next few steps may take a bit of your time. After completing the above series of steps, you’ll notice that new errors have populated the console window.

Oh no! More Errors!

Downloading NuGet packages typically downloads all the additional libraries necessary for them to function(this is because of reflection). You may notice within the Packages folder the amount of libraries downloaded alongside the Google NuGet package you’ve acquired. Copying the files into the Plugins folder (and editing your link.xml) must be done for all packages/libraries you downloaded and want to use. This Unity doc can help (look for the section titled “How to deal with stripping when using reflection”).

Wrapping Up

Once you’ve finished adding the library to the link.xml file and there are no longer any error messages in the Unity console window, your next step is testing. Depending on the API you’ve chosen this may be a bit difficult but the best way to test is to either use the API on a game element or print to the Unity console. I used the Translate API to translate “hello” to Spanish and print to the console.

Sources

Microsoft Documentation on Unity and .NET Framework 4.x

Unity Bytecode Stripping with IL2CPP

If it did not work, feel free to send me a message on Twitter and I will try to help out.

--

--

Jon Foust
Jon Foust

Written by Jon Foust

Developer Advocate @ Google. Gamer. Maker. Trying to make things easier for others by doing the hard stuff myself. Opinions are my own.

No responses yet