Chapter 10. Working with External Resource Files and Devices

In this chapter, we will cover:

  • Loading external resource files – using Unity Default Resources
  • Loading external resource files – by downloading files from the Internet
  • Loading external resource files – by manually storing files in the Unity Resources folder
  • Saving and loading player data – using static properties
  • Saving and loading player data – using PlayerPrefs
  • Saving screenshots from the game
  • Setting up a leaderboard using PHP/MySQL
  • Loading game data from a text file map
  • Managing Unity project code using Git version control and GitHub hosting
  • Publishing for multiple devices via Unity Cloud

Introduction

For some projects, it works fine to use the Inspector window to manually assign imported assets to the component slots, and then build and play the game with no further changes. However, there are also many times when external data of some kind can add flexibility and features to a game. For example, it might add updateable or user-editable content; it can allow memory of user preferences and achievements between scenes, and even game-playing sessions. Using code to read local or Internet file contents at runtime can help file organization and separation of tasks between game programmers and the content designers. Having an arsenal of different assets and long-term game memory techniques means providing a wide range of opportunities to deliver a rich experience to players and developers alike.

The big picture

Before getting on with the recipes, let's step back and have a quick review of the role of the asset files and the Unity game building and running process. The most straightforward way to work with assets is to import them into a Unity project, use the Inspector window to assign the assets to the components in the Inspector, and then build and play the game.

The big picture

Standalone executables offer another possible workflow, which is the adding of files into the Resources folder of the game after it has been built. This will support game media asset developers being able to provide the final version of assets after development and building has been completed.

However, another option is to use the WWW class to dynamically read assets from the web at runtime; or perhaps, for communication with a high score or multiplayer server, and sending and receiving information and files.

The big picture

When loading/saving data either locally or via the web interface, it is important to keep in mind the data types that can be used. When writing C# code, our variables can be of any type permitted by the language, but when communicated by the web interface, or to a local storage using Unity's PlayerPrefs class, we are restricted in the types of data that we can work with. Unity's WWW class permits three file types (text files, binary audio clips, and binary image textures), but, for example, for 2D UIs we sometimes need Sprite images and not Textures, so that we have provided in this chapter a C# method to create a Sprite from a Texture. When using the PlayerPrefs class, we are limited to saving and loading integers, floats, and strings. Similarly, when communicating with a web server using the URL encoded data, we are restricted to whatever we can place into strings (we include a PHP web-based high score recipe, where the user scores can be loaded and saved via such a method).

Finally, managing Unity project source code with an online Distributed Version Control System (DVCS) like Git and GitHub opens up new workflows for the continuous integration of code updates to the working builds. Unity Cloud will pull the updated source code projects from your online repository, and then build the game for designated versions of Unity and the deployment devices. Developers will get e-mails to confirm the build success, or to list the reasons for any build failure. The final two recipes in this chapter show you how to manage your code with Git and GitHub, and use Unity Cloud to build projects for multiple devices.

Note

Acknowledgement: Thanks to the following for publishing Creative Commons (BY 3.0) licensed icons: Elegant Themes, Picol, Freepik, Yannick, Google, www.flaticon.com.

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

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