Unity StreamingAssets,persistentDataPath文件夹

Unity StreamingAssets,persistentDataPath文件夹

Unity StreamingAssets 文件夹

Unity 中的大多数资源在构建时都会合并到项目中
将文件放入 Assets/StreamingAssets 文件夹使其可通过路径名访问有时会很有用。

配置文件 Version.txt

视频文件  xxx.MP4 

AssetBundle

放置在 Unity 项目中名为 StreamingAssets(区分大小写)的文件夹中的所有文件都将逐字复制到目标计算机上的特定文件夹。

在安装平台上StreamingAssets 路径下的文件是 只读

在非 Android平台上,此路径下的文件夹是非压缩的,可以使用支持 IO 的 File、FileStream、StreamReader、和 Unity 的 WWW 和 UnityWebRequest访问

在 Android 上,这些文件包含在压缩的 .jar 文件(其格式与标准的 zip 压缩文件基本相同)中。
这意味着,如果不使用 Unity 的 WWW 类来检索文件,则需要使用其他软件查看 .jar 存档内部并获取文件。

注意:在高版本的Unity 已经弃用 WWW 推荐使用 UnityWebRequest

在 Android 环境下 UnityWebRequest 加载本地文件需要在路径前加 file:// (切记)

Application.streamingAssetsPath 在 Android 环境下返回的路径带 jar:file:// 所以是可以直接使用UnityWebRequest

Application.persistentDataPath 在 Android 环境下返回的路径不带 file:// 所以当使用 UnityWebRequest 请求本地文件时,需要在前边拼接 file://

此文件夹的位置因平台而异

请注意,以下名称区分大小写:

可使用 Application.streamingAssetsPath 属性来获取 StreamingAssets 文件夹的位置,因为它总是指向运行应用程序的平台上的正确位置。

将路径获取封装

下面代码 

GetStreamingAssetsFilePath 方法 

GetStreamingAssetsFilePath2 方法

这两中方式均可,Unity 官方推荐直接使用:Application.streamingAssetsPath

using System.IO;
using UnityEngine;

namespace CommonUtils {

    public static class FileUtils {

        public static string GetStreamingAssetsFilePath(params string[] args) {
            string path = Application.streamingAssetsPath;
            string subPath = CombinePath(args);
            return CombinePath(path, subPath);
        }

        public static string GetStreamingAssetsFilePath2(params string[] args)
        {
            string path = string.Empty;
            string subPath = CombinePath(args);
            //获取不同设备上StreamingAssets文件夹路径     
#if UNITY_IPHONE     // Iphone路径
        path = path = Application.dataPath + "/Raw"; 
#elif UNITY_ANDROID  // Android路径
        path = "jar:file://" + Application.dataPath + "!/assets/"; 
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR   // PC路径
        path = Application.dataPath + "/StreamingAssets";
#endif

            return CombinePath(path, subPath);
        }

        public static string GetPersistentDataPath(params string[] args)
        {
            string path = Application.persistentDataPath;
            string subPath = CombinePath(args);
            return CombinePath(path, subPath);
        }

        /// <summary>
        /// Android 环境下
        /// 如果使用 UnityWebRequest 请求本地文件地址,需要在前边加上 file://  切记
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string GetPersistentWebUrl(params string[] args)
        {
            string path = GetPersistentDataPath(args);

#if UNITY_ANDROID  // Android路径
        path = "file://" + path; 
#endif
            return path;
        }

        public static string CombinePath(params string[] args)
        {
            return Path.Combine(args);
        }
    }
}

为了读取代码的统一性,建议使用 UnityWebRequest 加载文件

代码如下

    private void Load()
    {
        streamingFilePath = FileUtils.CombinePath(FileUtils.GetStreamingAssetsFilePath(), "version.txt");
        StartCoroutine(LoadFile(streamingFilePath));
    }

    /// <summary>
    /// 加载 streamingAssetsPath 路径下文件
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    IEnumerator LoadFile(string filePath)
    {
        UnityWebRequest request = UnityWebRequest.Get(filePath);
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.LogError("Error:" + request.error);
            yield break;
        }

        // 加载完成后通过两种方式读取数据

        // 通过 request.downloadHandler.text 直接获取所有文本
        string fileContent = request.downloadHandler.text;

        // 通过 request.downloadHandler.data 读取文件所有字节数据
        byte[] byteArray = request.downloadHandler.data;
        string msg = System.Text.Encoding.UTF8.GetString(byteArray);
    }

persistentDataPath

此文件夹不包含在项目开发目录,它是Android、iOS 等安装包被安装到设备上之后,自动生成的一个路径,根据平台不同,会生成不同路径

persistentDataPath 路径是一个持久化数据存储目录,覆盖安装和更新不会清除此路径下文件

此路径 可读、可写

不同平台上指向的路径如下

Windows Store Apps: Application.persistentDataPath points to %userprofile%\AppData\Local\Packages\<productname>\LocalState.

iOS: Application.persistentDataPath points to /var/mobile/Containers/Data/Application/<guid>/Documents.

Android: Application.persistentDataPath points to /storage/emulated/0/Android/data/<packagename>/files on most devices (some older phones might point to location on SD card if present), the path is resolved

在 iOS 上这个路径是应用程序的沙盒

在 Android 上 这个路径可以是沙盒,可可以是 SdCard,在 Android 打包的时候,ProjectSetting 界面有一个选项 Write Access,可以设置它的路径是 沙盒还是 SdCard

虽然 persistentDataPath 路径在任何平台上,可读、可写,不过只能在程序运行时才可 读写,提前将数据存入这个路径是不可行的

无内容限制,可以从 streamingAssetsPath 路径读取二进制文件、AssetBundle、文本文件等,然后写入 persistentDataPath 路径中

可以使用 File、WWW 方式读取

上面说 streamingAssetsPath 目录的时候,Android 是不能使用 File、FileStream、StreamReader 方式读取的,但是 persistentDataPath 路径不受此限制

写文件如下

    private void Write(byte[] bytes)
    {
        string persistentFilePath = FileUtils.GetPersistentDataPath("version.txt");
        if (File.Exists(persistentFilePath))
        {
            File.Delete(persistentFilePath);
        }

        File.WriteAllBytes(persistentFilePath, bytes);
    }

File 读取方式如下

    private void Read2()
    {
        // File 读取两种方式
        // 读取所有字节
        byte[] bytes = File.ReadAllBytes(persistentFilePath);

        // 读取所有文本
        string content = File.ReadAllText(persistentFilePath);
    }

上面的 FileUtils 代码部分已经包含了 persistentDataPath 路径获取方法  GetPersistentDataPath(params string[] args)

persistentDataPath 路径 UnityWebRequest 方式依然可以使用上面的 IEnumerator LoadFile(string filePath) 方法读取

persistentDataPath 作用:

1:游戏内一些需要存储在本地的数据,可以写入到这个路径中

     (1.1)比如上次一个界面,选择了哪个页签,下次打开这个界面,还要展示上次打开的页签

     (1.2)红点提示,当玩家获得道具,某个按钮显示红点,玩家点击后取消红点显示

2:AssetBundle 从 CDN 服务器上下载最新的 AssetBundle 文件,然后写入到本地

3:AppVersion、ResVersion 等版本文件,资源同步后,从服务器下载存入本地

等许多用途

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值