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.
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).
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):
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.
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).
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.
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.
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:
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.
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”).
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.
If it did not work, feel free to send me a message on Twitter and I will try to help out.