unity中 StreamingAssetsPath 文件夾和 PersistentDataPath 文件夾 區(qū)別

[圖片上傳中...(201908221448459.png-4481ef-1581838860210-0)]

streamingAssetsPath :

The path to the StreamingAssets folder(常用)

streamingAssetsPath返回的是流數(shù)據(jù)的緩存目錄,適合用來存儲一些外部數(shù)據(jù)文件用于讀取,一般是存放二進(jìn)制文件(比如:AssetBundle、.csv等)。StreamingAssets文件夾內(nèi)的東西不會被加密,放進(jìn)去什么就是什么,所以不要直接把數(shù)據(jù)文件赤裸裸地放到這個目錄下。一般不會去手動將數(shù)據(jù)寫入這個目錄。
在不同平臺上的路徑一覽:


20190822144553140.png

201908221448459.png

20190822145229192.png

在安卓平臺下該目錄下的文件被縮到一個單獨的.jar文件(類似于zip壓縮文件),可通過WWW讀取壓縮文件中的數(shù)據(jù)

string path =
#if UNITY_ANDROID && !UNITY_EDITOR
        Application.streamingAssetsPath; 
        //or 可以寫成 "jar:file://" + Application.dataPath + "!/assets";
       //安卓Application.streamingAssetsPath已默認(rèn)有"file://"
#elif UNITY_IOS && !UNITY_EDITOR
        "file://" + Application.streamingAssetsPath; 
         //or 寫成: "file://" + Application.dataPath +"/Raw";
#elif UNITY_STANDLONE_WIN || UNITY_EDITOR
        "file://" + Application.streamingAssetsPath;
           // or 寫成: "file://" + Application.dataPath + "/StreamingAssets";
#else
        string.Empty;
#endif
path = path + folderPathInStreamingAssets + "/" + fileName; //folderPathInStreamingAssets是子路徑
WWW www = new WWW(path);
yield return www; //等待www讀取完成

如果時安卓平臺需要對Application.streamingAssetsPath路徑下的文件進(jìn)行修改則需要把這個目錄下的文件復(fù)制到PersistentDataPath下路徑

/// 
    <summary>
   /// 安卓端復(fù)制文件
  /// </summary>
  /// <param name="fileName">文件路徑</param>
 /// <returns></returns>
 IEnumerator CopyFile_Android(string fileName)
{
     WWW w = new WWW(Application.streamingAssetsPath + "/" + fileName);
        yield return w;
      if (w.error == null)
      {
          FileInfo fi = new FileInfo(Application.persistentDataPath + "/" + fileName);
          //判斷文件是否存在
          if (!fi.Exists)
          {
              FileStream fs = fi.OpenWrite();
              fs.Write(w.bytes, 0, w.bytes.Length);
              fs.Flush();
              fs.Close();
              fs.Dispose();
               debug.log("CopyTxt Success!" + "\n" + "Path: ======> " +                    Application.persistentDataPath + fileName);
             }
     }
    else
     {
         log("Error : ======> " + w.error);
      }
}

persistentDataPath : Contains the path to a persistent data directory(常用)

persistentDataPaht
返回的是一個持久化數(shù)據(jù)存儲目錄。當(dāng)應(yīng)用程序發(fā)布到IOS和Android平臺,這個路徑會指向一個公共的路徑。應(yīng)用更新、覆蓋安裝時,這里的數(shù)據(jù)都不會被清除。
**
讀寫方式:可以用System.IO.StreamReader和System.IO.StreamWriter,也可用System.IO.File.ReadAllText和System.IO.File.WriteAllText。寫入之前,記得要先Directory.Exists(folderPath)判斷路徑是否存在。
注意:這個路徑比較特殊,
1.內(nèi)容可讀寫;
2.在IOS上就是應(yīng)用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard,并且在Android打包時,ProjectSetting頁面有一個選項Write Access,可以設(shè)置它的路徑是沙盒還是sdcard;
3.覆蓋安裝也不會清掉該目錄,該路徑可以拿來放一些數(shù)據(jù)(比如:從streamingAssetsPath路徑下讀取二進(jìn)制文件或者從streamingAssetsPath路徑下的AssetBundle中讀取文件來寫入PersistentDataPath、temporaryCachePath中)

**

temporaryCachePath : Contains the path to a temporary data / cache directory(IOS常用)

temporaryCachePath返回一個臨時數(shù)據(jù)緩存目錄。當(dāng)應(yīng)用程序發(fā)布到IOS和Android平臺,這個路徑也會指向一個公共的路徑。應(yīng)用更新、覆蓋安裝時,這里的數(shù)據(jù)都不會被清除,沒錯,就是不會,手機(jī)空間不足時才可能會被系統(tǒng)清除。
**
讀寫方式同persistentDataPath。
另外,IOS會自動將persistentDataPath路徑下的文件上傳到iCloud,會占用用戶的iCloud空間,如果persistentDataPath路徑下的文件過多,蘋果審核可能被拒,所以,IOS平臺,有些數(shù)據(jù)得放temporaryCachePath路徑下。
**

總結(jié)

這四個路徑中經(jīng)常用到的是:streamingAssetsPath和persistentDataPath。在IOS上會用到temporaryCachePath,而dataPath無用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容