C# Application类的dataPath、streamingAssetsPath、persistentDataPath、temporaryCachePath

总共有四个数据文件路径:dataPath、streamingAssetsPath、 persistentDataPath、temporaryCachePath,这四个路径都是相对路径,即对于不同的游戏平台返回的路径是不一样的。

一、dataPath : Contains the path to the game data folder(不常用)

dataPath是包含游戏数据文件夹的路径,是app程序包安装路径(安装应用时,系统自动将安装包拷贝到dataPath路径下进行安装),一般不会去手动读写这个目录。
在不同平台上的路径一览:
在这里插入图片描述
在这里插入图片描述
在不同平台上的路径详解:
1.UnityEditor:
路径:/Assets
路径举例:<path to project folder>/Assets
补充解释:该路径下的文件(不包括StreamingAssets和Resources路径)除非被引用,否则不会被打进包,所以数据文件可放入StreamingAssets和Resources路径而进包。
能否读:能
如何读:

//using System.IO;
//using System.Text;

string str = ReadInDataPath(string.Empty, "data.txt");

public static string ReadInDataPath(string folderPathInDataPath, string fileName) //folderPathInDataPath是子路径
{
	string fullPath = Application.dataPath + folderPathInDataPath+ "/" + fileName;
	//可用System.IO.File.ReadAllText(fullPath, Encoding.UTF8);这行替代下面这几行
    using (StreamReader sr = new StreamReader(fullPath, Encoding.UTF8))
    {
        return sr.ReadToEnd();
    }
}

能否写:能
如何写:

WriteInDataPath(string.Empty, "data.txt", "dataPath");

public static void WriteInDataPath(string folderPathInDataPath, string fileName, string content) 
{
	string folderPath = Application.dataPath + folderPathInDataPath;
	string fullPath = folderPath + "/" + fileName;
	if (!Directory.Exists(folderPath))
	{
	    Directory.CreateDirectory(folderPath);
	}
	//可用System.IO.File.WriteAllText(fullPath, content, Encoding.UTF8);这行替代下面这几行。默认是覆盖,如果要Apped就用AppendAllText
	using (StreamWriter sw = new StreamWriter(fullPath, false, Encoding.UTF8)) //第二个参数为fasle,表示覆盖文件
	{
	    sw.Write(content);
	}
}

这篇文章实现的都是同步读写,当然,也可以用协程或者用C#5.0提供的async await实现异步读写。至于什么时候需要异步读写:一帧内读写不完就可以考虑异步读写了。
2.Android:
路径:/data/app/xxx.xxx.xxx.apk
路径举例:/data/app/com.jump.runjump-vF55j3zfdrODFaqATyy1Fw==/base.apk
补充解释:此路径指向.apk文件,.apk是一个文件,不是目录,故不能读写
能否读:否
能否写:否
3.IOS:
路径:Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
路径举例:/var/mobile/Containers/Data/Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
补充解释:<path to player app bundle>/<AppName.app>/Data
能否读:能
如何读:略,没意义
能否写:否

二、streamingAssetsPath : The path to the StreamingAssets folder(常用)

streamingAssetsPath返回的是流数据的缓存目录,适合用来存储一些外部数据文件用于读取,一般是存放二进制文件(比如:AssetBundle、.csv等)。StreamingAssets文件夹内的东西不会被加密,放进去什么就是什么,所以不要直接把数据文件赤裸裸地放到这个目录下。一般不会去手动将数据写入这个目录。
在不同平台上的路径一览:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在不同平台上的路径详解:
1.UnityEditor:
路径:/Assets/StreamingAssets
路径举例:<path to project folder>/Assets/StreamingAssets
能否读:能
能否写:能
2.Android:
路径:jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
路径举例:jar:file:///data/app/com.jump.runjump-vF55j3zfdrODFaqATyy1Fw==/base.apk!/assets
补充解释:该目录下的文件被压缩到一个单独的.jar文件(类似于zip压缩文件),可通过WWW读取压缩文件中的数据
能否读:能
能否写:否(但可采用第三方压缩/解压类库实现读写)
3.IOS:
路径:Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
路径举例:/var/containers/Bundle/Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
能否读:能
能否写:能

上述三种平台如何读:
方式1:不同平台下,都用WWW进行数据读取(WWW只能异步读取数据,但只能用协程实现异步,而不能用async await实现异步,因为WWW和协程都是Unity实现的,而async await是C#5.0提供的,WWW还不支持async await)

	string path =
#if UNITY_ANDROID && !UNITY_EDITOR
		Application.streamingAssetsPath; //安卓的Application.streamingAssetsPath已默认有"file://"
#elif UNITY_IOS && !UNITY_EDITOR
		"file://" + Application.streamingAssetsPath;
#elif UNITY_STANDLONE_WIN || UNITY_EDITOR
		"file://" + Application.streamingAssetsPath;
#else
		string.Empty;
#endif
path = path + folderPathInStreamingAssets + "/" + fileName; //folderPathInStreamingAssets是子路径
WWW www = new WWW(path);
yield return www; //等待www读取完成
string testData = www.text;

方式2:和方式1一样,只是路径拼接方式不同

	string path =
#if UNITY_ANDROID && !UNITY_EDITOR
		"jar:file://" + Application.dataPath + "!/assets"; //安卓下,该目录下的文件被压缩到一个单独的.jar文件供WWW读取
#elif UNITY_IOS && !UNITY_EDITOR
		"file://" + Application.dataPath +"/Raw";
#elif UNITY_STANDLONE_WIN || UNITY_EDITOR
		"file://" + Application.dataPath + "/StreamingAssets";
#else
		string.Empty;  
#endif
//后面的代码和方式1相同,故略

方式3:不同平台下,Android用WWW进行数据读取,UnityEditor和IOS用System.IO进行数据读取

string path = Application.streamingAssetsPath;
path = path + folderPathInStreamingAssets + "/" + fileName;
string testData = "";
if (path.Contains("://"))
{
    WWW www = new WWW(path);
    yield return www; //等待www读取完成
    testData = www.text;
}
else
{
    testData = System.IO.File.ReadAllText(path, Encoding.UTF8);
}
三、persistentDataPath : Contains the path to a persistent data directory(常用)

persistentDataPaht返回的是一个持久化数据存储目录。当应用程序发布到IOS和Android平台,这个路径会指向一个公共的路径。应用更新、覆盖安装时,这里的数据都不会被清除。
在不同平台上的路径一览:
在这里插入图片描述
在这里插入图片描述
在不同平台上的路径详解:
1.UnityEditor:
路径:C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
路径举例:C:/Users/jumpchen/AppData/LocalLow/jump/runjump
能否读:能
能否写:能
2.Android:
路径:/data/data/xxx.xxx.xxx/files
路径举例:/storage/emulated/0/Android/data/com.jump.runjump/files
补充解释:安卓机上看的路径:内部存储/Android/data/com.jump.runjump/files 如下图
在这里插入图片描述
能否读:能
能否写:能
3.IOS:
路径:Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
路径举例:/var/mobile/Containers/Data/Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
能否读:能
能否写:能

读写方式:可以用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平台,这个路径也会指向一个公共的路径。应用更新、覆盖安装时,这里的数据都不会被清除,没错,就是不会,手机空间不足时才可能会被系统清除。
在不同平台上的路径一览:
在这里插入图片描述
在这里插入图片描述
在不同平台上的路径详解:
1.UnityEditor:
路径:C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName
路径举例:C:/Users/jumpchen/AppData/Local/Temp/jump/runjump
能否读:能
能否写:能
2.Android:
路径:/data/data/xxx.xxx.xxx/cache
路径举例:/storage/emulated/0/Android/data/com.jump.runjump/cache
补充解释:安卓机上看的路径:内部存储/Android/data/com.jump.runjump/cache
能否读:能
能否写:能
3.IOS:
路径:Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
路径举例:/var/mobile/Containers/Data/Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
能否读:能
能否写:能
读写方式同persistentDataPath。
另外,IOS会自动将persistentDataPath路径下的文件上传到iCloud,会占用用户的iCloud空间,如果persistentDataPath路径下的文件过多,苹果审核可能被拒,所以,IOS平台,有些数据得放temporaryCachePath路径下。

总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值