unity中 StreamingAssetsPath 文件夹和 PersistentDataPath 文件夹 区别

[图片上传中...(201908221448459.png-4481ef-1581838860210-0)]

streamingAssetsPath :

The path to the StreamingAssets folder(常用)

streamingAssetsPath返回的是流数据的缓存目录,适合用来存储一些外部数据文件用于读取,一般是存放二进制文件(比如:AssetBundle、.csv等)。StreamingAssets文件夹内的东西不会被加密,放进去什么就是什么,所以不要直接把数据文件赤裸裸地放到这个目录下。一般不会去手动将数据写入这个目录。
在不同平台上的路径一览:


20190822144553140.png

201908221448459.png

20190822145229192.png

在安卓平台下该目录下的文件被缩到一个单独的.jar文件(类似于zip压缩文件),可通过WWW读取压缩文件中的数据

string path =
#if UNITY_ANDROID && !UNITY_EDITOR
        Application.streamingAssetsPath; 
        //or 可以写成 "jar:file://" + Application.dataPath + "!/assets";
       //安卓Application.streamingAssetsPath已默认有"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路径下的文件进行修改则需要把这个目录下的文件复制到PersistentDataPath下路径

/// 
    
   /// 安卓端复制文件
  /// 
  /// 文件路径
 /// 
 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
返回的是一个持久化数据存储目录。当应用程序发布到IOS和Android平台,这个路径会指向一个公共的路径。应用更新、覆盖安装时,这里的数据都不会被清除。
**
读写方式:可以用System.IO.StreamReader和System.IO.StreamWriter,也可用System.IO.File.ReadAllText和System.IO.File.WriteAllText。写入之前,记得要先Directory.Exists(folderPath)判断路径是否存在。
注意:这个路径比较特殊,
1.内容可读写;
2.在IOS上就是应用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard,并且在Android打包时,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard;
3.覆盖安装也不会清掉该目录,该路径可以拿来放一些数据(比如:从streamingAssetsPath路径下读取二进制文件或者从streamingAssetsPath路径下的AssetBundle中读取文件来写入PersistentDataPath、temporaryCachePath中)

**

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

temporaryCachePath返回一个临时数据缓存目录。当应用程序发布到IOS和Android平台,这个路径也会指向一个公共的路径。应用更新、覆盖安装时,这里的数据都不会被清除,没错,就是不会,手机空间不足时才可能会被系统清除。
**
读写方式同persistentDataPath。
另外,IOS会自动将persistentDataPath路径下的文件上传到iCloud,会占用用户的iCloud空间,如果persistentDataPath路径下的文件过多,苹果审核可能被拒,所以,IOS平台,有些数据得放temporaryCachePath路径下。
**

总结

这四个路径中经常用到的是:streamingAssetsPath和persistentDataPath。在IOS上会用到temporaryCachePath,而dataPath无用。

你可能感兴趣的:(unity中 StreamingAssetsPath 文件夹和 PersistentDataPath 文件夹 区别)