应用环境本次只包含,pc,安卓,editor,三种
先把三个环境下的路径配置好.
#if UNITY_EDITOR
_path = Application.dataPath + "/Data/";
#elif UNITY_EDITOR_WIN
_path = Application.dataPath + "/Data/";
#elif UNITY_ANDROID
_path = Application.persistentDataPath+"/NameTableData/";
#endif
UNITY_EDITOR: Assets目录下
UNITY_EDITOR_WIN: 生成exe的目录下包名_Data 目录下
UNITY_ANDROID: 安装apk后生成在.com.包名目录下
创建目录:
if (!Directory.Exists(_path))
Directory.CreateDirectory(_path);
创建文本:
if (!File.Exists(dmData))
{
var fs = File.Create(dmData);
fs.Close();
}
写入数据:
using (var sw = File.AppendText(dmData))
{
sw.WriteLine("写入的内容");
sw.Close();
}
清空数据:
FileStream fs = File.Open(dmData, FileMode.OpenOrCreate, FileAccess.Write);
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
fs.Close();
读取数据:
StreamReader sr;
sr = File.OpenText(dmData);
string strData = sr.ReadToEnd();
sr.Close();