Game Development Reference
In-Depth Information
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class LocalHiscore : Hiscore {
private string[] RANDOM_NAMES = new string[]
{"Antony", "John", "Will", "Kate", "Jill"};
public override void SaveUserData ( UserData user )
{
_users.Add(user);
BinaryFormatter binary = new BinaryFormatter();
MemoryStream memory = new MemoryStream();
binary.Serialize(memory, _users);
PlayerPrefs.SetString(_hashKey+"Highscore",Convert.ToBase64String(memory.GetBuffer()));
}
}
We created the save function by first adding the new user to the list, and then we
created BinaryFormatter to serialize all users data to MemoryStream . Fin-
ally, we saved the users data by converting it to a string and saving it in Play-
erPrefs .
3. Next, add two more functions— Initialize() and LoadUserData() —to
set and load the user data. Let's add both the methods as follows:
// Unity JavaScript user:
override function Initialize ( maxUser : int ) {
super.Initialize(maxUser);
LoadUserData();
}
override function LoadUserData () {
super.LoadUserData();
Search WWH ::




Custom Search