安卓打包后,配置文件无法读取成功
查找资料后,根本看不懂www读取的方法,尴尬!
Unity在Android平台下读取配置文件有以下几种方法:
- 使用Application.persistentDataPath读取写在PersistentData路径下的文件。这个路径在Android上对应/data/data//files路径。可以这样读取:
string configFilePath = Path.Combine(Application.persistentDataPath, "config.txt");
if (File.Exists(configFilePath))
{
string configContents = File.ReadAllText(configFilePath);
} - 使用Application.streamingAssetsPath读取写在StreamingAssets文件夹下的文件。这个文件夹的内容会打包到apk里,对于不需要写的配置文件很方便。读取方式如下:
string configFilePath = Path.Combine(Application.streamingAssetsPath, "config.txt");
if (File.Exists(configFilePath))
{
string configContents = File.ReadAllText(configFilePath);
} - 将文件打包到Resources文件夹下,通过Resources.Load读取:
TextAsset textAsset = Resources.Load("config");
string configContents = textAsset.text; - 最简单的就是将文件打包到Assets文件夹下,然后通过AssetDatabase.LoadAssetAtPath读取:
TextAsset textAsset = AssetDatabase.LoadAssetAtPath("Assets/config.txt");
string configContents = textAsset.text;
配置文件读取就是这些常用的方法,可以根据需求选择最适合的方式。
不知道你这个问题是否已经解决, 如果还没有解决的话:
如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^