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读取文件的方法,适用于只需要对于文件进行简单操作和读取的情况.
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D中,我们可以使用C#的文件读写方法来实现在安卓读取文件的功能。 首先,我们需要将要读取文件放置在安卓设备的存储空间中。可以使用Android的API方法来实现文件的存储。例如,可以使用Unity的Application.persistentDataPath属性来获得设备存储空间的路径,然后使用System.IO命名空间下的File类来创建和写入文件。 接下来,要在安卓设备上读取文件,我们需要使用System.IO命名空间下的File类和StreamReader类。首先,通过File.OpenText方法打开文件,该方法接受文件的完整路径作为参数。然后,使用StreamReader来读取文件内容。StreamReader的ReadLine方法可以按行读取文件内容。 读取文件内容后,记得要关闭StreamReader和File,释放资源。 以下是一个简单示例代码,展示了在Unity3D中如何读取安卓设备上的文本文件: ```c# using System.IO; using UnityEngine; public class AndroidFileReader : MonoBehaviour { void Start() { string filePath = Path.Combine(Application.persistentDataPath, "example.txt"); ReadFile(filePath); } private void ReadFile(string path) { if (File.Exists(path)) { using (StreamReader sr = File.OpenText(path)) { string line; while ((line = sr.ReadLine()) != null) { Debug.Log(line); } } } else { Debug.Log("File does not exist."); } } } ``` 上述代码将会从设备存储空间中读取名为"example.txt"的文件,并逐行输出文件内容到Unity控制台中。 当在Unity3D中开发安卓应用时,很重要的一点是要确保获取到的路径是设备存储空间的路径,即通过Application.persistentDataPath属性获得的路径。这样才能保证在不同的安卓设备上都能正常读取文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值