定义一个类存储玩家的数据,进行序列化。读取时以流的方式,反序列化后进行读取。
前言
我这边做的是一个类似跳一跳的移动端小游戏,里面的游戏商店可以利用每局累计获得的钻石购买玩家皮肤,
关闭游戏,再次进入后,保存玩家的所有数据,比如钻石数和游戏道具(已经解锁的皮肤),在这一阶段,需要用到数据持久化存储。
定义GameData类专门用于存储游戏数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable] //定义标签可对数据进行序列化
public class GameData
{
public static bool IsAgainGame = false; //是否再来一次游戏
//需要序列化的属性
private bool isFirstGame;
private bool isMusicOn;
private int[] bestScoreArr;
private int selectSkin;
private bool[] skinUnlocked;
private int diamondCount;
//Set方法
public void SetIsFirstGame(bool isFirstGame)
{
this.isFirstGame = isFirstGame;
}
public void SetIsMusicOn(bool isMusicOn)
{
this.isMusicOn = isMusicOn;
}
public void SetBestScoreArr(int[] bestScoreArr)
{
this.bestScoreArr = bestScoreArr;
}
public void SetSelectSkin(int selectSkin)
{
this.selectSkin = selectSkin;
}
public void SetSkinUnlocked(bool[] skinUnlocked)
{
this.skinUnlocked = skinUnlocked;
}
public void SetDiamondCount(int diamondCount)
{
this.diamondCount = diamondCount;
}
//GET方法
public bool GetIsFirstGame()
{
return isFirstGame;
}
public bool GetIsMusicOn()
{
return isMusicOn;
}
public int[] GetBestScoreArr()
{
return bestScoreArr;
}
public int GetSelectSkin()
{
return selectSkin;
}
public bool[] GetSkinUnlocked()
{
return skinUnlocked;
}
public int GetDiamondCount()
{
return diamondCount;
}
}
GameManager中定义Save和Read方法使用文件流序列化和反序列化GameData中的数据
/// <summary>
/// 存储数据
/// </summary>
private void Save()
{
try
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = File.Create(Application.persistentDataPath + "/GameData.data"))
//using会自动释放流,不然需要添加fs.Close()
{
data.SetBestScoreArr(bestScoreArr);
data.SetDiamondCount(diamondCount);
data.SetIsFirstGame(isFirstGame);
data.SetIsMusicOn(isMusicOn);
data.SetSelectSkin(selectSkin);
data.SetSkinUnlocked(skinUnlocked);
bf.Serialize(fs, data);//将类中数据序列化后写入本地
}
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
}
/// <summary>
/// 读取数据
/// </summary>
private void Read()
{
try
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = File.Open(Application.persistentDataPath + "/GameData.data",FileMode.Open))
{
data = (GameData)bf.Deserialize(fs);
}
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
}
GameManager中定义初始化游戏数据方法,引用GameData中需要序列化的数据
private bool isFirstGame;
private bool isMusicOn;
private int[] bestScoreArr;
private int selectSkin;
private bool[] skinUnlocked;
private int diamondCount;
/// <summary>
/// 初始化游戏数据
/// </summary>
private void InitGameData()
{
Read();
if(data != null)
{
isFirstGame = data.GetIsFirstGame();
}
else
{
isFirstGame=true;
}
//如果是第一次开始游戏
if(isFirstGame)
{
isMusicOn = true;
bestScoreArr = new int[3];
selectSkin = 0;
skinUnlocked = new bool[vars.skinSpriteList.Count];
skinUnlocked[0] = true;
diamondCount = 10;
data = new GameData();
Save();
}
else
{
isMusicOn = data.GetIsMusicOn();
bestScoreArr = data.GetBestScoreArr();
selectSkin = data.GetSelectSkin();
skinUnlocked = data.GetSkinUnlocked();
diamondCount = data.GetDiamondCount();
}
}