1莹桅、 Unity自身提供的PlayerPrefs
//保存數(shù)據(jù)
PlayerPrefs.SetString("Name",mName);
PlayerPrefs.SetInt("Age",mAge);
PlayerPrefs.SetFloat("Grade",mGrade)
//讀取數(shù)據(jù)
mName=PlayerPrefs.GetString("Name","DefaultValue");
mAge=PlayerPrefs.GetInt("Age",0);
mGrade=PlayerPrefs.GetFloat("Grade",0F);
//清除所有記錄
PlayerPrefs.DeleteAll();
//刪除其中某一條記錄
PlayerPrefs.DeleteKey("Age");
//將記錄寫(xiě)入磁盤(pán)
PlayerPrefs.Save()
2沽讹、 BinaryFormatter 二進(jìn)制序列化
///假設(shè)有一個(gè)Player類(lèi)
[System. Serializable]
public class Player
{
public int health;
public int power;
public Vector3 position;
}
///由于BinaryFormatter序列化不支持Unity的Vector3類(lèi)型近刘,所以我們需要做一下包裝娘荡。
public class PlayerData{
public int level;
public int health;
public float[] position;
public PlayerData(Player player)
{
this.level = player.level;
this.health = player.health;
this.position = new float[3];
this.position[0] = player.transform.position.x;
this.position[1] = player.transform.position.y;
this.position[2] = player.transform.position.z;
}
}
///我們對(duì)PlayerData進(jìn)行保存和讀取钞护。讀取出來(lái)的PlayerData可以賦給Player喉祭。
public static class SaveSystem{
//保存數(shù)據(jù)
public static void SavePlayer(Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath+"/player.fun";
FileStream stream = new FileStream(path,FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream,data);
stream.Close();
}
//讀取數(shù)據(jù)
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath+"/player.fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path,FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}else{
Debug.LogError("Save file not found in "+path);
return null;
}
}
}
3养渴、 保存為json格式的文本文件
使用 Unity 自身API JsonUtility。
//保存數(shù)據(jù)
public static void SavePlayerJson(Player player)
{
string path = Application.persistentDataPath+"/player.json";
var content = JsonUtility.ToJson(player,true);
File.WriteAllText(path,content);
}
//讀取數(shù)據(jù)
public static PlayerData LoadPlayerJson()
{
string path = Application.persistentDataPath+"/player.json";
if(File.Exists(path)){
var content = File.ReadAllText(path);
var playerData = JsonUtility.FromJson<PlayerData>(content);
return playerData;
}else{
Debug.LogError("Save file not found in "+path);
return null;
}
}
4泛烙、 XmlSerializer進(jìn)行串行化
//假如有類(lèi)
public class Entity
{
public Entity()
{
}
public Entity(string c, string f)
{
name = c;
school = f;
}
public string name;
public string school;
}
//讀取數(shù)據(jù)
List<Entity> entityList=null;
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
using (StreamReader sr = new StreamReader(configPath))
{
entityList = xs.Deserialize(sr) as List<Entity>;
}
//保存數(shù)據(jù)
List<Entity> entityList=null;
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
using (StreamWriter sw = File.CreateText(configPath))
{
xs.Serialize(sw, entityList);
}
//對(duì)應(yīng)的xml文件為:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity>
<Name>Alice</Name>
<School>SJTU</School>
</Entity>
<Entity>
<Name>Cici</Name>
<School>CSU</School>
</Entity>
<Entity>
<Name>Zero</Name>
<School>HIT</School>
</Entity>
</ArrayOfEntity>