In this chapter, we will talk about saving and loading the high score from the local machine or web server.
Why do we need to save the high score? The advantage of the high score is to keep a record of the players and how well they progress each time they play the game. It also creates a challenge for the players to beat their record and keep playing the game again. For the online game, the high score is very important to let the players see their progress and compare with their friends or other players.
We will create a simple high score table so that the players can save the score locally as well as post their score to the server database, by using the example project that is in this chapter.
This chapter is basically the extension of the project in Project 7, Creating a Destructible and Interactive Virtual World, and it includes the new RESTART button (using the OnGUI
function instead of the GUITexture button). It also includes the time UI and score UI for the player to see the result when they complete the game.
We will start by creating the high score menu for saving and loading the score from our local machine by using the PlayerPrefs
class in Unity, which allows us to save the parameters locally to our machine (this is very similar to SharedObject
in ActionScript 3.0).
Then, we will create the C# XMLParser
script that will be used to get the XML value of the return data from the provided database server. Next, we will create the Unity Javascript
, which will handle the posting and loading of scores from the database server.
Finally, we need to encrypt our high score by using the MD5 encryption class written by Matthew Wegner. For more details please refer to the following website:
http://www.unifycommunity.com/wiki/index.php?title=MD5.
The MD5 encryption script will allow us to encrypt the hash key, which will prevent submission of fake high scores.
When we finish this chapter, we will have the GAME OVER menu, which has four buttons to allow the players to submit their score on their local machine and server database, to load the high score data from the player's local machine, to load the high score from the server database, and a RESTART button to replay the game.
What we will get from this chapter is the way to use PlayerPrefs
to save high score. This PlayerPrefs
also allows us to save game data, such as the location of the player, current stage, or current hit points. We will learn how to set up the basic database server by using MySQL and PHP script to return the high score data in the XML format to the game. We will also look at how to create the C# script to parse our XML data for using our high score table. Lastly, we will get to know how to use MD5 script to encrypt the user data before sending to the server database.
We will start by importing the chapter8
package, which we will download in the next section, and then begin creating the high score table with the following topics:
First, we need the chapter8
package. We can start by going to this URL: http://www.packtpub.com/support?nid=8267 to download the Chapter8.zip
package. The package will contain all the necessary resources such as assets, scripts, and prefabs that we will be using for this project.
Then, we import the package to Unity as we did for the other chapters, as shown in the following screenshot:
In the Project view, we will see the Chapter5, Chapter6, Chapter7, Chapter8, Gizmos folders, the HiScore scene, and Standard Assets. The Chapter5, Chapter6, and Chapter7 folders will contain all the scripts, assets, terrain, and prefabs, which we will use from the previous chapters. The Chapter8 folder will have the Box.psd
file, HiScore.php
script, and a scripts
folder that contains the HiScore.js
script, which we will use in this chapters. The Gizmos folder will contain the wayIcon.png
file. Standard Assets contains all the necessary built-in assets that will be used in the chapter. Finally, HiScore is the scene we will use for this chapter.
Double-click on the HI-SCORE scene to open the scene for Chapter8.
18.191.235.176