Unity_Android读取txt文件的简单方法

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.streamingAssetsPathjar: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读取文件的方法,适用于只需要对于文件进行简单操作和读取的情况.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值