10 Connecting your game to the internet

 

This chapter covers

  • Generating dynamic visuals for the sky
  • Downloading data using web requests in coroutines
  • Parsing common data formats like XML and JSON
  • Displaying images downloaded from the internet
  • Sending data to a web server

In this chapter, you’ll learn how to send and receive data over a network. The projects built in previous chapters represented a variety of game genres, but all have been isolated to the player’s machine. As you know, connecting to the internet and exchanging data is increasingly important for games in all genres. Many games exist almost entirely over the internet, with constant connection to a community of other players; games of this sort are referred to as MMOs (massively multiplayer online) and are most widely known through MMORPGs (MMO role-playing games). Even when a game doesn’t require such constant connectivity, modern video games usually incorporate features like reporting scores to a global list of high scores, or record analytics to help improve the game. Unity provides support for such networking, so we’ll be going over those features.

Unity supports multiple approaches to network communication, since different approaches are better suited to different needs. But, this chapter will mostly cover the most general sort of internet communication: issuing HTTP requests.

10.1 Creating an outdoor scene

10.1.1 Generating sky visuals using a skybox

10.1.2 Setting up an atmosphere that’s controlled by code

10.2 Downloading weather data from an internet service

10.2.1 Requesting HTTP data using coroutines

10.2.2 Parsing XML

10.2.3 Parsing JSON

10.2.4 Affecting the scene based on weather data

10.3 Adding a networked billboard

10.3.1 Loading images from the internet

10.3.2 Displaying images on the billboard

10.3.3 Caching the downloaded image for reuse

10.4 Posting data to a web server

10.4.1 Tracking current weather: sending post requests

10.4.2 Server-side code in PHP

Summary