UIFramework之Unity中的Path详解

UIFramework之Unity中的Path详解

iOS

Application.dataPath :  Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data 

(dataPath是app程序包安装路径,app本身就在这里,此目录是只读的)

Application.streamingAssetsPath :   Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw   (只读)

Application.persistentDataPath :      Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents               (沙盒路径,可以将热更新的AssetBundle,配置文件,数据表等文件放在该路径下)
Application.temporaryCachePath :   Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches         (相对临时的目录,适合存放下载缓存的临时文件,空间不足时可能会被系统清除)

Android

apk程序包目录: apk的安装路径,/data/app/package name-n/base.apk,dataPath就是返回此目录。
内部存储目录: /data/data/package name-n/,用户自己或其它app都不能访问该目录。打开会发现里面有4个目录(需要root) 
1、cache缓存目录,类似于iOS的Cache目录 
2、databases数据库文件目录 
3、files类似于iOS的Documents目录 (android平台上直接使用该路径当做沙盒路径)
4、shared_prefs 类似于iOS的Preferences目录,用于存放常用设置,比如Unity3D的PlayerPrefs就存放于此
外部存储目录: 在内置或外插的sd上,用户或其它app都可以访问,外部存储目录又分私有和公有目录。 
公有目录是像DCIM、Music、Movies、Download这样系统创建的公共目录,当然你也可以像微信那样直接在sd卡根目录创建一个文件夹。好处嘛,就是卸载app数据依旧存在。

私有目录在/storage/emulated/n/Android/data/package name/,打开可以看到里面有两个文件夹cache和files。为什么跟内部存储目录重复了?这是为了更大的存储空间,以防内存存储空间较小。推荐把不需要隐私的、较大的数据存在这里,而需要隐私的或较小的数据存在内部存储空间。
Application.dataPath : /data/app/xxx.xxx.xxx.apk   (apk程序包目录)
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets    (只读)
Application.persistentDataPath :         /data/data/xxx.xxx.xxx/files                                             

(沙盒路径,可以将热更新的AssetBundle,配置文件,数据表等文件放在该路径下)
Application.temporaryCachePath :      /data/data/xxx.xxx.xxx/cache                                     

Windows

Application.dataPath :                         /Assets              (工程路径)                                                                   
Application.streamingAssetsPath :      /Assets/StreamingAssets
Application.persistentDataPath :         C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
Application.temporaryCachePath :      C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName

Mac

Application.dataPath :                         /Assets
Application.streamingAssetsPath :      /Assets/StreamingAssets
Application.persistentDataPath :         /Users/xxxx/Library/Caches/CompanyName/Product Name
Application.temporaryCachePath :     /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name


PS

1、StreamingAssets 只能用过www类来读取。
安卓上跟其他平台不一样,安装后,这些文件实际上是在一个Jar压缩包里,所以不能直接用读取文件的函数去读,而要用WWW方式。具体做法如下:

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. byte[] InBytes;                                                                                                         
  2.  //用来存储读入的数据   
  3. if (Application.platform == RuntimePlatform.Android)                                             
  4.  //判断当前程序是否运行在安卓下   
  5. {   
  6.         string FileName = "jar:file://" +   
  7. Application.dataPath + "!/assets/" + "文件.txt";   
  8.         WWW www = new WWW(FileName);                                                             
  9.  //WWW会自动开始读取文件   
  10.         while(!www.isDone){}                                                                         
  11. //WWW是异步读取,所以要用循环来等待   
  12.         InBytes = www.bytes;                                                                         
  13. //存到字节数组里   
  14. }   
  15. else   
  16. {   
  17.        //其他平台的读取代码   
  18. }  
2、根目录:StreamingAssets文件夹

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #if UNITY_EDITOR  
  2. string filepath = Application.dataPath   
  3. +"/StreamingAssets"+"/my.xml";  
  4. #elif UNITY_IPHONE  
  5.  string filepath = Application.dataPath   
  6. +"/Raw"+"/my.xml";  
  7. #elif UNITY_ANDROID  
  8.  string filepath = "jar:file://"   
  9. + Application.dataPath + "!/assets/"+"/my.xml;  
  10. #endif  
[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. * Name: GameFileUtil.cs 
  3. * Function: N/A  
  4.  
  5. * Ver     变更日期               负责人                            变更内容 
  6. * ────────────────────────────────────────────────────────────────────── 
  7. * V1.0.0  $time$    http://blog.csdn.net/husheng0      
  8.  
  9. * Copyright (c). All rights reserved. 
  10. * 
  11. * 游戏中的路径相关管理类  
  12.  
  13. */  
  14.   
  15. using UnityEngine;  
  16. using System.Collections;  
  17. using System.IO;  
  18. using System;  
  19.   
  20. public class GameFileUtil : SingletonManager<GameFileUtil>  
  21. {  
  22.  
  23. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN  
  24.     public const string FileProto = "file:///";  
  25. #else  
  26.     public const string FileProto = "file://";  
  27. #endif  
  28.     /// <summary>  
  29.     /// 返回沙盒路径  
  30.     /// Android:假设文件都存在手机上,不存在SD卡,返回路径  
  31.     /// Application.persistentDataPath :         /data/data/xxx.xxx.xxx/files   
  32.     /// iOS:返回路径Application.persistentDataPath :  
  33.     /// Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents  
  34.     /// PC:返回路径Application.dataPath的上一级路径        /Assets/../   
  35.     /// </summary>  
  36.     /// <param name="type">文件类型,对应沙盒路径下的文件名字</param>  
  37.     /// <param name="fileName">文件名</param>  
  38.     /// <param name="useType">使用类型</param>  
  39.     /// <returns></returns>  
  40.     public string GetFilePath(GameFileType type, string fileName)  
  41.     {  
  42.         string filePath = Application.dataPath + "/../"   
  43.             +type.ToString()+ fileName.ToString();  
  44. #if UNITY_ANDROID||UNITY_IPHONE  
  45.         filePath = Application.persistentDataPath    
  46.         + "/" + type.ToString()+ fileName.ToString();  
  47. #endif  
  48.         switch (type)  
  49.         {  
  50.             case(GameFileType.AssetBundles):  
  51.                 filePath = filePath + ".assetbundle";  
  52.                 break;  
  53.             case(GameFileType.Config):  
  54.                 filePath = filePath + ".ini";  
  55.                 break;  
  56.             case(GameFileType.CapturePicture):  
  57.                 filePath = filePath + ".png";  
  58.                 break;  
  59.             case(GameFileType.Datas):  
  60.                 filePath = filePath + ".csv";  
  61.                 break;  
  62.             case(GameFileType.NetDatas):  
  63.                 filePath = filePath + ".xml";  
  64.                 break;  
  65.             default:  
  66.                 break;  
  67.         }  
  68.         return filePath;  
  69.     }  
  70.   
  71.     public string GetStreamingAssetsPath()  
  72.     {  
  73.         string filePath = Application.dataPath + "/StreamingAssets/";  
  74. #if UNITY_ANDROID||UNITY_IPHONE  
  75.         filePath = Application.streamingAssetsPath+"/";  
  76. #endif  
  77.         return filePath;  
  78.     }  
  79.   
  80.     public string GetAssetFilePath(GameFileType fileType,  
  81.         string fileName, GameFileUseType useType)  
  82.     {  
  83.         string filePath = null;  
  84.         switch (useType)  
  85.         {  
  86.             case(GameFileUseType.Read):  
  87.             case(GameFileUseType.Write):  
  88.                 if (File.Exists(GetFilePath(fileType, fileName)))  
  89.                 {  
  90.                     filePath = GetFilePath(fileType, fileName);  
  91.                 }  
  92.                 break;  
  93.             case(GameFileUseType.CreatePathRead):  
  94.             case(GameFileUseType.CreatePathWrite):  
  95.                 if (File.Exists(GetFilePath(fileType, fileName)))  
  96.                 {  
  97.                     filePath = GetFilePath(fileType, fileName);  
  98.                 }  
  99.                 else  
  100.                 {  
  101.                     try  
  102.                     {  
  103.                         FileStream stream = new FileStream(filePath, FileMode.CreateNew);  
  104.                         filePath = GetFilePath(fileType, fileName);  
  105.                     }  
  106.                     catch (Exception e)  
  107.                     {  
  108.                         Debug.LogError(e.Message);  
  109.                     }  
  110.                 }  
  111.                 break;  
  112.             default:  
  113.                 break;  
  114.         }  
  115.         return filePath;  
  116.     }  
  117.   
  118.     /// <summary>  
  119.     /// 使用WWW加载文件的路径  
  120.     /// </summary>  
  121.     /// <param name="fileType">文件类型</param>  
  122.     /// <param name="subFilePath">在其文件类型下的相对目录,定位到文件名,不使用/开头</param>  
  123.     public string GetWWWLoadPath(GameFileType fileType, string fileName,  
  124.         GameFileUseType useType = GameFileUseType.Read)  
  125.     {  
  126.         string filePath = GetAssetFilePath(fileType, fileName, useType);  
  127.         if (string.IsNullOrEmpty(filePath))  
  128.         {  
  129.             return null;  
  130.         }  
  131.         return FileProto + filePath;  
  132.     }  
  133. }  
  134. /// <summary>  
  135. /// 文件类型枚举  
  136. /// </summary>  
  137. public enum GameFileType  
  138. {  
  139.     AssetBundles,       //AssetBundle文件  
  140.     Datas,              //数据表文件  
  141.     Config,             //配置文件  
  142.     CapturePicture,     //截屏文件  
  143.     NetDatas,           //网络数据缓存文件  
  144. }  
  145. /// <summary>  
  146. /// 文件使用类型  
  147. /// </summary>  
  148. public enum GameFileUseType  
  149. {  
  150.     Read,           // 读文件  
  151.     CreatePathRead, // 读文件,如果文件所在的目录不存在,会自动创建目录  
  152.     Write,          // 写文件  
  153.     CreatePathWrite,// 写文件,如果文件所在的目录不存在,会自动创建目录  
  154. }  

=====================================================================

结束。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值