Unity中:
StreamingAssets的文件夹,特点是:只能读,不能写.
persistentDataPath可读可写(但是,此文件夹在Editor阶段没有,手机安装App后自动生成)
可以将一些必需的文件先放在StreamingAssets,在App安装后Copy到persistentDataPath文件夹
public void copy(string fileName = "test.txt")
{
//文件地址
string url;
//Mac,Windows或Linux平台
#if UNITY_EDITOR || UNITY_STANDALONE
url = $"file://{Application.dataPath}/StreamingAssets/{fileName}";
//ios平台路径
#elif UNITY_IPHONE
url = $"file://{Application.dataPath}/Raw/{fileName}";
//安卓路径
#elif UNITY_ANDROID
url = $"jar:file://{Application.dataPath}!/assets/{fileName}";
#endif
string persistentUrl = $"{Application.persistentDataPath}/{fileName}";
if (!File.Exists(persistentUrl))
{
Debug.Log($"{persistentUrl} 文件不存在,从StreamingAssets中Copy!");
WWW www = new WWW(url);
while (true)
{
if (www.isDone)
{
if (www.error == null)
{
//本次读的文本
File.WriteAllText(persistentUrl, www.text);
File.WriteAllBytes(path, www.bytes);
Debug.Log($"持久化目录: {persistentUrl}");
break;
}
else
{
Debug.LogWarning($"没得到StreamingAssets的文件 : {fileName}");
}
}
}
}
else
{
Debug.Log($"{persistentUrl} 文件已存在!");
}
}
PC路径:C:/Users/admin/AppData/LocalLow/DefaultCompany/OurTest/PublicObj/test.txt
安卓:/storage/emulated/0/Android/data/com.OurTest.PublicObj/files/test.txt
PS:项目中用到调用win自带软件调取对应路径文件的功能,经测试,路径指定时不能是
“***/***/***/*******.***”形式
要改为“***\\***\\***\\*******.***”,如果遇到类似的问题,可以尝试修改看看。