Game Development Reference
In-Depth Information
Engage thrusters
Now we can start creating the LocalHiscore script using the following steps:
1. Navigate to Chapter8 | Scripts | C#/Hiscore (for C# users) or Chapter8 | Scripts
| Javascript | Hiscore (for Unity JavaScript users), right-click and navigate to
Create | C# Script (for C# users) or Create | Javascript , and name it
LocalHiscore .
2. First, we will create a random name for the default user and the SaveUser-
Data() function to save the user data. Let's double-click on the LocalHiscore
script that we just created and use the following code:
// Unity JavaScript user:
#pragma strict
import System;
import System.Runtime.Serialization.Formatters.Binary;
import System.IO;
import System.Collections.Generic;
class LocalHiscore extends Hiscore {
private var RANDOM_NAMES : String[] = ["Antony",
"John", "Will", "Kate", "Jill"];
override function SaveUserData ( user : UserData ) {
_users.Add(user);
var binary : BinaryFormatter = new
BinaryFormatter();
var memory : MemoryStream = new MemoryStream();
binary.Serialize(memory, _users);
PlayerPrefs.SetString(_hashKey+"Highscore",Convert.ToBase64String(memory.GetBuffer()));
}
}
// C# user:
Search WWH ::




Custom Search