1.PC端读取Resources文件夹下的.txt文件,并解析分为一行一行存入ArrayList
2.Android端如果文件不存在则在缓存区创建一个.txt,并将Resources文件夹下.test.txt文件信息复制写入新创建的.txt中
/// <summary>
/// 获取当前平台路径
/// </summary>
void GetPlatformPath()
{
if(Application .platform ==RuntimePlatform.Android )
{
PathSource = Application.persistentDataPath;
//test.txt,文件流写入和创建需要文件后缀名
}
else if(Application.platform==RuntimePlatform.WindowsEditor||Application .platform ==RuntimePlatform.WindowsPlayer )
{
PathSource = Application.dataPath;
//testpc端Resources读取不需要后缀名
}
}
//android端方法
ArrayList FunAndroid()
{
StreamWriter wt;
FileInfo t = new FileInfo(PathSource + "//" + ConfigPath);
if (!t.Exists)
{
wt = t.CreateText();
TextAsset te = Resources.Load(ConfigPathName) as TextAsset;
string strs = Encoding.UTF8.GetString(te.bytes);
if (te != null)
{
wt.Write(strs, 0, 1024);
wt.Close();
wt.Dispose();
}
Debug.Log("-----------dont have ,creat one");
}
else
{
StreamReader sr = null;
try
{
sr = File.OpenText(PathSource + "//" + ConfigPath);
}
catch (System.Exception e)
{
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
arrlist.Add(line);
}
sr.Close();
sr.Dispose();
return arrlist;
}
return null;
}
//pc端方法
ArrayList FunPc()
{
TextAsset te = Resources.Load(ConfigPathName) as TextAsset;
if (te != null)
{
string tempStr = te.text;
string[] Strs = tempStr.Split('\n');
if (Strs.Length > 0)
{
ArrayList arrlist = new ArrayList();
for (int i = 0; i < Strs.Length; i++)
{
arrlist.Add(Strs[i]);
}
return arrlist;
}
return null;
}
else
{
return null;
}
}
//联合
ArrayList GetFile()
{
if(Application .platform ==RuntimePlatform.Android )
{
FunAndroid();
}
else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
FunPc();
}
return null;
}
//临时给.txt写入信息
public void Test()
{
StreamWriter wt;
//在文件后面添加写入信息
FileInfo fi = new FileInfo(PathSource + "//" + ConfigPath);
wt = fi.AppendText();//如果直接重新写入,不需要在原文件后面追加内容则用fi.CreateText ();
string strs = "\n"+"ab,25,100";
wt.WriteLine(strs);
wt.Close();
wt.Dispose();
Debug.Log("-----------add -------------");
}