Unity_Android读取txt文件的简单方法
Unity版本:2018.4.22
提要:将StreamingAssets中的txt文件读取后写入PersistentDataPath中.待新txt生成后便可以随意修改txt文件.
目标
需要在Android端读取一个能够临时修改的txt文件.
知识点
Unity中四种资源路径
路径 | 说明 |
---|---|
Application.dataPath | 此属性用于返回程序的数据文件所在文件夹的路径。例如在Editor中就是Assets了。 |
Application.streamingAssetsPath | 此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。放在Unity工程StreamingAssets文件夹中的资源发布后都可以通过这个路径读取出来。 |
Application.persistentDataPath | 此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。 |
Application.temporaryCachePath | 此属性用于返回一个临时数据的缓存目录。 |
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 |
Android中的注意点
- StreamingAssets 文件夹只读,不可写.且只能通过WWW读取.
- PersistentDataPath 可读可写,但是只有在运行时能够读写.
思路
根据上述两个注意点得出可以采用的方法
将StreamingAssets中的txt文件读取后写入PersistentDataPath中.待新txt生成后便可以随意修改txt文件.
代码
using UnityEngine;
using System.IO;
- 第零步:从StreamingAssets中读取文件并写入persistentDataPath
/// <summary>
/// 从StreamingAssets中读取文件并写入persistentDataPath
/// </summary>
/// <param name="file_name">文件名</param>
public static void CopyFileFromStreamingAsset(string file_name)
{
string from_path;
if (Application.platform == RuntimePlatform.Android)
from_path = "jar:file://" + Application.dataPath + "!/assets/" + file_name;
else
from_path = Application.streamingAssetsPath + file_name;
string to_path = Application.persistentDataPath + "/" + file_name;
WWW www = new WWW(from_path);
while (!www.isDone) { }
if (www.error == null)
//这里也可以加个判断文件是否已经存在再进行写入
File.WriteAllBytes(to_path, www.bytes);
}
- 第一步 判断文件是否存在并读取文件
/// <summary>
/// 读取文件中所有line
/// </summary>
/// <param name="file_name"></param>
/// <returns></returns>
public static string[] ReadFileLines(string file_name)
{
string path = Application.persistentDataPath + "/" + file_name;
if (!File.Exists(path)) CopyFileFromStreamingAsset(file_name);
return File.ReadAllLines(path);
}
}
- 后续
- 其他读取整个文件或者写入文件的方法
public static string ReadFile(string file_name)
{
string path = Application.persistentDataPath + "/" + file_name;
if (!File.Exists(path)) CopyFileFromStreamingAsset(file_name);
return File.ReadAllText(path);
}
public static void WriteFile(string file_name, string str)
{
string path = Application.persistentDataPath + "/" + file_name;
File.WriteAllText(file_name, str);
}
public static void WriteFileLines(string file_name, string[] str_list)
{
string path = Application.persistentDataPath + "/" + file_name;
File.WriteAllLines(file_name, str_list);
}
- 从SteamingAssets中读取文件的方法
public static string ReadFileFromStreamingAsset(string file_name)
{
string from_path;
if (Application.platform == RuntimePlatform.Android)
from_path = "jar:file://" + Application.dataPath + "!/assets/" + file_name;
else
from_path = Application.streamingAssetsPath + file_name;
WWW www = new WWW(from_path);
while (!www.isDone) { }
if (www.error == null) return www.text;
else return www.error;
}
总结
- 这是一种最为简单的Android读取文件的方法,适用于只需要对于文件进行简单操作和读取的情况.