Chapter 7. Working with External APIs

In previous chapters you created a multiplayer air hockey game. One way of expanding it would be making more levels and mechanics for it, but you already know how to do that. The other way would be integrating it with different external services, such as analytics, online scoring platforms, and leaderboards.

In this chapter we are going to talk about application programming interfaces (APIs). We will cover the following topics:

  • API—what it is and what it's used for
  • Existing useful external APIs
  • The way Unity typically communicates with external APIs
  • Integrating a game with one of the existing APIs (Kongregate)

We will look at some code snippets that you are going to use to integrate your game with Kongregate, test the game online, and save the number of times the player wins on Kongregate's servers.

About external application programming interfaces

In simple terms and in the context of Unity, an external API is an external library of code that can be accessed from a Unity script and provides some additional functionality to your game. Some APIs let you access the JavaScript code of the page your WebPlayer game is on, while others provide a possibility of transferring game data and getting information to remote servers.

You have already used one API in your air hockey game in the previous chapter: that of Photon Unity Networking. Most of the calls to remote servers are buried deep inside of its source code, but it is nevertheless an API.

Other APIs that you might encounter include online game platforms, such as Kongregate and Facebook; analytics tools, such as Google Analytics and Game Analytics; and online data storage platforms, such as Scoreoid and Steamworks.

In addition to different functions, APIs use different ways to connect to the external code base and different ways to communicate with it later, which may seem like a hard task, but rarely is. There is generally a comprehensive guide to the API on its website, and even when there is not the Unity community often comes to the rescue, putting together its own guides, template files, and code snippets readily available in Unity answers or forums.

We are going to integrate our game with Kongregate to show how this works and what kind of code you need to use. Kongregate was chosen as a fairly straightforward, very common, and completely free API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.24