Unity路径的相关总结

一、Unity中的路径变量

  1. Application.dataPath
    应用数据文件夹的路径,只读。
  2. Application.streamingAssetsPath
    应用的流式数据缓存目录,该目录下可以保存一些外部数据文件。
  3. Application.temporaryCachePath
    设备的临时数据缓存目录。
  4. Application.persistentDataPath
    iOS/Android设备中的持久化数据存储目录,可以在此路径下存储一些持久化的数据文件,该目录下的文件不会因为App升级而删除。

二、不同平台下的路径

| 路径变量 | Windows | Android | iOS |
| :------------ | :---------------- | :------------ | :---------- | :---------- |
| Application.dataPath | 应用的路径/appname _Data | /data/app/package name-1/base.apk | /var/containers/Bundle/Application/app sandbox/xxx.app/Data |
| Application.streamingAssetsPath | 应用路径/appname _Data /StreamingAssets | jar:file:///data/app/package name-1/base.apk!/assets | /var/containers/Bundle/Application/app sandbox/test.app/Data/Raw |
| Application.temporaryCachePath | C:\Users\username\AppData\Local\Temp\company name\product name | Internal Only: /data/user/0/package name/cache External(SDCard): /storage/emulated/0/Android/data/package name/cache | /var/mobile/Containers/Data/Application/app sandbox/Library/Caches |
| Application.persistentDataPath | C:\Users\username\AppData\LocalLow\company name\product name | Internal Only: /data/user/0/ package name/files External(SDCard): /storage/emulated/0/Android/data/package name/files | /var/mobile/Containers/Data/Application/app sandbox/Documents |

说明:Android平台下的路径会根据SD卡的访问权限不同而不同。至于iOS有没有类似的情况,由于没有相关设备,暂时没有测试。有条件的朋友可以帮我测试一下,我用5.3.4f1版本的Unity写了个简单的测试程序,项目链接:http://pan.baidu.com/s/1gfqmORh。 欢迎测试反馈,不胜感激!

三、常用工具方法

    /// <summary>
    /// 获取不同平台的流式加载路径
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <returns></returns>
    public static string GetStreamingFilePath(string filename)
    {
        string path = "";

        if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer ||
            Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            path = Application.dataPath + "/StreamingAssets/" + filename;
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            path = Application.dataPath + "/Raw/" + filename;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            path = "jar:file://" + Application.dataPath + "!/assets/" + filename;
        }
        else
        {
            path = Application.dataPath + "/config/" + filename;
        }

        return path;
    }

    /// <summary>
    /// 获取不同平台的持久化数据存储路径
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <returns></returns>
    public static string GetPersistentFilePath(string filename)
    {
        string filepath = Application.persistentDataPath + "/" + filename;

#if UNITY_IPHONE
        // Set file flag to be excluded from iCloud/iTunes backup.
        UnityEngine.iOS.Device.SetNoBackupFlag(filepath);
#endif
        return filepath;
    }

四、参考文献

  1. Unity3D各平台Application.xxxPath的路径
  2. Unity3D移动平台动态读取外部文件全解析
  3. Unity各种路径
  4. Unity官方文档

本文作者: Sheh伟伟
本文链接: http://davidsheh.github.io/2017/02/09/Unity路径的相关总结/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,948评论 25 708
  • 前言 我们在用Unity开发的过程中经常会遇到从本地加载资源以及保存资源到本地这样的需求,Unity也提供了集中本...
    bibishou阅读 694评论 0 0
  • 来源:http://blog.csdn.net/ynnmnm/article/details/52253674 h...
    洪福齐天999阅读 7,883评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,949评论 19 139
  • 一山望一山高,站在山头一般高,这是我妈对经常跳槽的我,老念叨的一句话。 事实证明呢,她说的很对,当自己在某个行业,...
    态渡阅读 285评论 0 1

友情链接更多精彩内容