1.PlayerPrefs;
存储数据
1 2 3 4 5 6 7
| PlayerPrefs.SetString(“Name”,”Tony”);
PlayerPrefs.SetInt(“Age”,11);
PlayerPrefs.SetFloat(“Score”,123.5f);
PlayerPrefs.Save();
|
读取数据
1 2 3 4 5 6 7
| PlayerPrefs.GetString(“Name”);
Playerf.GetInt(“Age”);
PlayerPrefs.GetFloat(“Score”);
PlayerPrefs.HasKey(“Score”);
|
2.序列化与反序列化
Serialization(序列化) 将对象转化为字节流;
Deseriallzation(反序列化) 将字节流转化为对象;
二进制
序列化:新建或打开一个二进制文件,通过二进制格式器将对象写入二进制文件;
反序列化:打开待反序列化的文件,通过二进制格式器将文件解析成对象;
//简单,可读性差;
二进制保存
1 2 3 4 5 6 7 8 9 10 11 12
| Save save = CreateSaveObject();
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(Application.dataPath + "/StreamingFile" + "/ByBin.dat");
bf.Serialize(fs, save); fs.Close(); if(File.Exists(Application.dataPath+ "/StreamingFile" + "/ByBin.dat")) { Debug.Log("二进制保存成功"); }
|
二进制读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| stringfilePath= Application.dataPath + "/StreamingFile" + "/ByBin.dat"; if(File.Exists(filePath)) { BinaryFormatter bf = new BinaryFormatter(); FileStream fs =File.Open(filePath,FileMode.Open); Save save =(Save)bf.Deserialize(fs); fs.Close(); SetGame(save); } else { Debug.Log("找不到游戏档案!"); }
|
XML
可读性强,文件大,冗余信息多;
序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| Save save = CreateSaveObject(); string path =Application.dataPath + "/StreamingFile" + "/ByXml.xml"; XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("body");
root.SetAttribute("name", "GameData"); XmlElement animal, pos, type;
for(int i=0;i<save.livingTarget.Count;i++) { animal = xmlDoc.CreateElement("animal"); pos = xmlDoc.CreateElement("pos"); pos.InnerText =save.livingTarget[i].ToString(); type = xmlDoc.CreateElement("type"); type.InnerText =save.livingType[i].ToString();
animal.AppendChild(pos); animal.AppendChild(type);
root.AppendChild(animal);
}
XmlElement score = xmlDoc.CreateElement("score"); score.InnerText = save.Score.ToString(); root.AppendChild(score);
XmlElement shootNum = xmlDoc.CreateElement("shootNum"); shootNum.InnerText = save.ShootNumber.ToString();
root.AppendChild(shootNum);
xmlDoc.AppendChild(root);
xmlDoc.Save(path); if(File.Exists(path)) { Debug.Log("Xml保存成功"); }
|
反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| string path =Application.dataPath + "/StreamingFile" + "/ByXml.xml"; if(File.Exists(path)) { Save save = new Save(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path);
XmlNodeList list =xmlDoc.GetElementsByTagName("animal");
if(list.Count!=0) { foreach(XmlNode node in list) { XmlNode targetPos =node.ChildNodes[0]; int index = int.Parse(targetPos.InnerText); save.livingTarget.Add(index);
XmlNode targetType =node.ChildNodes[1]; int type = int.Parse(targetType.InnerText); save.livingType.Add(type); } }
XmlNodeList shootNum =xmlDoc.GetElementsByTagName("shootNum"); int targetShootNum = int.Parse(shootNum[0].InnerText); save.ShootNumber = targetShootNum;
XmlNodeList score =xmlDoc.GetElementsByTagName("score"); int targetScore = int.Parse(score[0].InnerText); save.Score = targetScore;
SetGame(save); }
|
Json
//格式简单,易于读写,但是不够直观,可读性比XML差;
1.引用LitJson.dll
序列化
1 2 3 4 5 6 7 8 9 10 11 12
| Save save = CreateSaveObject(); string path =Application.dataPath + "/StreamingFile" + "/byJson.json";
stringstr_json = JsonMapper.ToJson(save);
StreamWriter sw = new StreamWriter(path); sw.Write(str_json); sw.Close(); if(File.Exists(path)) { Debug.Log("Json存储完成"); }
|
反序列化
1 2 3 4 5 6 7 8 9 10
| string path=Application.dataPath + "/StreamingFile" + "/byJson.json"; if(File.Exists(path)) { StreamReader sr = new StreamReader(path); string str_json = sr.ReadToEnd(); sr.Close(); Save save =JsonMapper.ToObject<Save>(str_json); SetGame(save); }
|
用到的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| private Save CreateSaveObject() { Save save = new Save(); foreach(GameObjecttargetGO intargetGOs) { TargetManager targetManager =targetGO.GetComponent<TargetManager>(); if(targetManager.activeMonster!=null) { save.livingTarget.Add(targetManager.TargetPositionIndex); int type =targetManager.activeMonster.GetComponent<MonsterManager>().MonstorType; save.livingType.Add(type); } } save.ShootNumber = UIManager._instance.shootNum; save.Score = UIManager._instance.score; return save; }
[System.Serializable]
public class Save{ publicList<int>livingTarget = newList<int>(); publicList<int>livingType = newList<int>(); publicint ShootNumber = 0; publicint Score = 0; }
|
//2018.4.6
//TonyChen