Game Development Reference
In-Depth Information
public function GetName() : String {
return s_name;
}
public function GetScore() : int {
return int_score;
}
2. We just created the Init() funcion to set up the score and name from this object.
Next, we will create the SaveLocal() funcion, which will get the index and save
the name and score to our local machine by using PlayerPref :
//Saving Data
public function SaveLocal (index : int) : void {
//Saving user score
PlayerPrefs.SetInt(keylocal + s_keyScore + index.ToString(),
int_score);
//Saving user name
PlayerPrefs.SetString(keylocal + s_keyName + index.ToString(),
s_name);
}
3. Then, we will create the LoadLocal() , LoadScore() , and LoadName()
funcions, which will load the user's score and name from the index:
//Loading Data
public function LoadLocal (index : int) : void {
int_score = LoadScore(index);
s_name = LoadName(index);
}
private function LoadScore (index : int) : int {
//Checking to see if the value already exists
var s_newKey : String = keylocal + s_keyScore + index.
ToString();
if (PlayerPrefs.HasKey(s_newKey)) {
return PlayerPrefs.GetInt(keylocal + s_keyScore + index.
ToString());
} else {
//If no key exist return 0 score
return 0;
}
}
private function LoadName (index : int) : String {
 
Search WWH ::




Custom Search