[转]本地存储,序列化与反序列化

      在没有服务器的情况下,有时候我们希望游戏数据进度能保存下来,这是就需要使用本地存储技术了,今天我们就来聊怎么实现游戏数据本地缓存吧。

      本地存储,把需要缓存的数据存储为一个文件,存储文件时需要将它序列化;当需要缓存数据时就读取该文件,将其反序列化得到需要的数据。

下面通过实例具体介绍一下实现方法

(1)定义一个游戏数据类为GameData,用序列化存储需要在类前写上 [System.Serializable]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class GameData
{
	...
}

(2)定义游戏中需要保存的数据

    private bool isFirstGame;//是否是第一次游戏(必要变量)
    private bool isMusicOn;
    private int[] bestScoreArr;//保存前三名
    private int diamondCount;//砖石数量

(3)定义设置(2)中变量的方法

    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 SetDiamondCount(int diamondCount)
    {
        this.diamondCount = diamondCount;
    }

(4)定义获取(2)中变量的方法

    public bool GetIsFirstGame()
    {
        return this.isFirstGame;
    }
    public bool GetisMusicOn()
    {
        return this.isMusicOn;
    }
    public int[] GetBestScoreArr()
    {
        return this.bestScoreArr;
    }
    public int GetDiamondCount()
    {
        return this.diamondCount;
    }

(5)在初始化游戏的地方定义保存数据,读取数据的方法,这里选用在GameMananger脚本

    private void Save()
    {
        try
        {
        	//需要引入命名空间using
            BinaryFormatter bf = new BinaryFormatter(); System.Runtime.Serialization.Formatters.Binary;
            //需要引入命名空间using System.IO;
            using (FileStream fs = File.Create(Application.persistentDataPath + "/GameData.data"))//文件保存的名字和路径
            {
                data.SetBestScoreArr(bestScoreArr);
                data.SetDiamondCount(diamondCount);
                data.SetIsFirstGame(isFirstGame);
                data.SetIsMusicOn(isMusicOn);

                bf.Serialize(fs,data);//序列化
            }
        }
        catch (System.Exception e)
        {

            Debug.Log(e.Message);
        }
    }


    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);
        }
    }

(6)在GameMananger中定义一个方法来初始化游戏数据,命名为InitGameData。先读数据,如果数据为空,则是首次进入,首次进入需要初始化数据并保存;非空则不是首次进入,则直接取数据。

    private void InitGameData()
    {
        Read();
        if (data != null)
        {
        	//不是首次游戏
            isFirstGame = data.GetIsFirstGame();
        }
        else
        {
        //首次游戏
            isFirstGame = true;
        }

        if (isFirstGame)
        {
        	//初始化数据
            isMusicOn = true;
            bestScoreArr = new int[3];
            diamondCount = 0;
            Save();//初始化完需要保存数据
        }
        else
        {
        	//读取缓存数据
            isMusicOn = data.GetisMusicOn();
            bestScoreArr = data.GetBestScoreArr();
            diamondCount = data.GetDiamondCount();
        }
    }

(7)在Awake中调用InitGameData()方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值