在Unity中首先創建一個Save腳本,用於保存遊戲中相關的信息,在這裏我存儲的是遊戲中怪物的位置以及類型、遊戲射擊總數量、遊戲得分等數據,Save腳本的代碼如下(這個腳本是不能掛在在場景中的遊戲物體上,因爲這個類沒有繼承自MonoBehaviour行爲類):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//串行化,串行化是指存儲和獲取磁盤文件、內存或其他地方中的對象。
[System.Serializable]
public class Save {
public List<int> livingTargetPosition = new List<int>();
public List<int> livingMonsterTypes = new List<int>();
public int shootNum = 0;
public int score = 0;
}
然後再創建一個腳本GameManager,並放置以下的方法:
//創建Save對象並存儲當前遊戲狀態信息
private Save CreateSaveGO()
{
//新建Save對象
Save save = new Save();
//遍歷所有的Target
//如果其中有激活狀態的怪物,就把該target的位置信息和激活狀態的怪物的類型添加到List中
//targetGOs是存儲遊戲中所有怪物的一個一維數組
foreach (GameObject targetGO in targetGOs)
{
TargetManager targetManager = targetGO.GetComponent<TargetManager>();
//當這個數組中某一個元素中有怪物的時候
if(targetManager.target != null)
{
save.livingTargetPosition.Add(targetManager.targetPosition);
int type = targetManager.target.GetComponent<MonsterManager>().monsterType;
save.livingMonsterTypes.Add(type);
}
}
//把shootNum和socre保存到Save對象中
save.shootNum = UIManager._instance.shootNum;
save.score = UIManager._instance.scoreNum;
//返回Save對象
return save;
}
下面就是重點來了,開始二進制的存檔和讀檔部分!
二進制的存檔方法:
private void SaveByBin()
{
//序列化過程(將Save對象轉換爲字節流)
//創建Save對象並保存當前遊戲狀態
Save save = CreateSaveGO();
//創建一個二進制格式化程序
BinaryFormatter bf = new BinaryFormatter(); //引入命名空間using System.Runtime.Serialization.Formatters.Binary;
//創建一個文件流(就是在Assets中創建的一個文件夾名稱爲StreamFile,然後系統會給我創建一個byBin的文本文檔用於保存信息)
FileStream fileStream = File.Create(Application.dataPath + "/StreamFile" + "/byBin.txt"); //引入命名空間using System.IO;
//用二進制格式化程序的序列化方法來序列化Save對象,參數:創建的文件流和需要系列化對象
bf.Serialize(fileStream, save);
//關閉流
fileStream.Close();
//檢測這個二進制文件是否存在,也就是是否保存成功
if (File.Exists(Application.dataPath + "/StreamFile" + "/byBin.txt"))
{
//這是在遊戲場景中給出文字提示
UIManager._instance.ShowMessages("保存成功");
}
}
二進制的讀檔方法:
private void LoadByBin()
{
//如果該二進制文件存在的話
if(File.Exists(Application.dataPath + "/StreamFile" + "/byBin.txt"))
{
//反系列化的過程
//創建一個二進制格式化程序
BinaryFormatter bf = new BinaryFormatter();
//打開一個文件流
FileStream fileStream = File.Open(Application.dataPath + "/StreamFile" + "/byBin.txt", FileMode.Open);
//調用格式化程序的反序列化方法,將文件流轉換爲一個Save對象
Save save = (Save)bf.Deserialize(fileStream);
//關閉文件流
fileStream.Close();
//調用激活怪物以及更新分數的UI方法
SetGame(save);
}
else
{
UIManager._instance.ShowMessages("存檔文件不存在");
}
}
根據讀出的信息,進行激活怪物,以及更新場景分數UI的SetGame方法:
private void SetGame(Save save)
{
//先將所有的target裏面的怪物清空,並重置所有的計時
foreach (GameObject targetGo in targetGOs)
{
targetGo.GetComponent<TargetManager>().UpdateMonsters();
}
//通過反系列化得到的Save對象中存儲的信息,激活指定的怪物
for (int i = 0; i < save.livingTargetPosition.Count; i++)
{
int position = save.livingTargetPosition[i];
int type = save.livingMonsterTypes[i];
targetGOs[position].GetComponent<TargetManager>().ActivateMonsterByType(type);
}
//更新UI提示
UIManager._instance.shootNum = save.shootNum;
UIManager._instance.scoreNum = save.score;
//調整爲未暫停狀態
UnPause();
}
好了,如果還有不懂得地方可以去這個網址觀看完整版的數據存檔和讀檔!(http://www.sikiedu.com/my/course/129),如果您覺得這篇文章寫的還可以的話,不妨動動您的小手指,給俺點個贊,謝謝大家!