程序篇01:Unity数据持久化 - kudan-game/ArtArtist-Repo GitHub Wiki
本篇介绍如何将Unity数据持久化
方式一:PlayerPrefs——Unity自带的持久化方法
PlayerPrefs:它的工作原理是以键值对的形式将数据保存在文件中。
//保存A_name, m_inputField.text键值对
PlayerPrefs.SetString("A_name", m_inputField.text);
//读取A_name键的值,default name对应空值
m_inputField.text = PlayerPrefs.GetString("A_name","default name");
PlayerPrefs是一个持久化数据的字典存储格式,发现这个方法是可以保存到硬盘文件的(windows是注册表中。。),而且使用是可以跨Scene的。
方式二:Application.persistentDataPath——Unity提供的应用持久化数据路径
persistentDataPath:此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。
例如先声明目录文件全路径:
private string pathFile = Application.persistentDataPath + "/redapple.txt";
实例输出的路径为:
C:/Users/heave/AppData/LocalLow/DefaultCompany/New Unity Project/redapple.txt
完整读写代码实例如下:
private string pathFile = Application.persistentDataPath + "/redapple.txt";
private string strIP = null;
private string strPort = null;
public string[] pData;//存放两个持久化数据
//读取数据
public bool ReadData()
{
bool ret = false;
if (ReadFromFile(pathFile, 2, ref pData))
{
strIP = pData[0];
strPort = pData[1];
ret = true;
}
return ret;
}
//读取文件
public bool ReadFromFile(string path, int length, ref string[] data)
{
StreamReader sr = new StreamReader(path);
for (int i = 0; i < length; i++)
{
data[i] = sr.ReadLine();
}
sr.Close();
return true;
}
//写入数据
public bool WriteData(string strip, string strport)
{
bool ret = false;
pData[0] = strip;
pData[1] = strport;
if (WriteToFile(pathFile, 2, pData))
{
ret = true;
}
return ret;
}
//写入文件
public bool WriteToFile(string path, int length, string[] data)
{
StreamWriter sw = new StreamWriter(path);
sw.Write("");
for (int i = 0; i < length; i++)
{
sw.WriteLine(data[i]);
}
sw.Close();
return true;
}
附:Unity3d中相应各平台Path
IOS:
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
Android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache
Windows:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
Application.temporaryCachePath : C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName
Mac:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : /Users/xxxx/Library/Caches/CompanyName/Product Name
Application.temporaryCachePath : /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name
Windows Web Player:
Application.dataPath : file:///D:/MyGame/WebPlayer (即导包后保存的目录,html文件所在目录)
Application.streamingAssetsPath :
Application.persistentDataPath :
Application.temporaryCachePath :