Game Development Reference
In-Depth Information
Engage Thrusters
We will create the ServerHiScore script to send and load the user data to the server,
which is also encrypted with the MD5 .
1. Let's go to Assets | Create | JavaScript and name it ServerHiScore . Then, we will
double-click it to open MonoDevelop and add the following code:
//Setting the PHP url here
public var PHPUrl : String = "http://www.jatewit.com/Packt/
HiScore.php";
//Setting the hash key id
public var hashKey : String = "UNITYGAMEDEVELOPMENTHOTSHOT";
private var obj_WWW : WWWForm;
private var b_loaded : boolean;
public function Start() : void {
// Empty Check for Inspector values
if( PHPUrl == "" ) {
Debug.LogError( "PHP Url cannot be null." );
}
if( hashKey == "" ) {
Debug.LogError( "Hash Key cannot be null." );
}
}
In the preceding script, we created the parameter to set the PHP URL that we will be
connected to (we won't see anything if we try to view the link in our browser), set
the hash key to check for the user, and create the WWWFrom and boolean objects to
use in this script. In the Start() funcion, we just checked to make sure that the
PUPUrl and hashKey are not null.
2. Then, create the SendScore(score : int, name :String) funcion, which
will take two parameters score and name . This funcion will create the WWWFrom ,
set the parameter, and send it to the URL that we just assigned. Let's type the
funcion as follows:
//Creating the function to send
public function SendScore( score : int, name : String) : void {
var w_form : WWWForm = new WWWForm();
//Telling PHP that the user is submitting the data
w_form.AddField("action", "PostScore");
//Sending hash code key to prevent unwanted user
w_form.AddField("hash", MD5.Md5Sum(name + "-" + score.ToString()
+ "-" + hashKey)); //Encrypt with MD5
//Sending the user score
 
Search WWH ::




Custom Search