一、Unity中的路徑變量
- Application.dataPath
應(yīng)用數(shù)據(jù)文件夾的路徑,只讀樟遣。 - Application.streamingAssetsPath
應(yīng)用的流式數(shù)據(jù)緩存目錄,該目錄下可以保存一些外部數(shù)據(jù)文件。 - Application.temporaryCachePath
設(shè)備的臨時(shí)數(shù)據(jù)緩存目錄含懊。 - Application.persistentDataPath
iOS/Android設(shè)備中的持久化數(shù)據(jù)存儲(chǔ)目錄,可以在此路徑下存儲(chǔ)一些持久化的數(shù)據(jù)文件衅胀,該目錄下的文件不會(huì)因?yàn)锳pp升級(jí)而刪除岔乔。
二、不同平臺(tái)下的路徑
| 路徑變量 | Windows | Android | iOS |
| :------------ | :---------------- | :------------ | :---------- | :---------- |
| Application.dataPath | 應(yīng)用的路徑/appname _Data | /data/app/package name-1/base.apk | /var/containers/Bundle/Application/app sandbox/xxx.app/Data |
| Application.streamingAssetsPath | 應(yīng)用路徑/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 |
說(shuō)明:Android平臺(tái)下的路徑會(huì)根據(jù)SD卡的訪問(wèn)權(quán)限不同而不同滚躯。至于iOS有沒(méi)有類似的情況雏门,由于沒(méi)有相關(guān)設(shè)備,暫時(shí)沒(méi)有測(cè)試掸掏。有條件的朋友可以幫我測(cè)試一下茁影,我用5.3.4f1版本的Unity寫(xiě)了個(gè)簡(jiǎn)單的測(cè)試程序,項(xiàng)目鏈接:http://pan.baidu.com/s/1gfqmORh丧凤。 歡迎測(cè)試反饋募闲,不勝感激!
三愿待、常用工具方法
/// <summary>
/// 獲取不同平臺(tái)的流式加載路徑
/// </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>
/// 獲取不同平臺(tái)的持久化數(shù)據(jù)存儲(chǔ)路徑
/// </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;
}
四浩螺、參考文獻(xiàn)
- Unity3D各平臺(tái)Application.xxxPath的路徑
- Unity3D移動(dòng)平臺(tái)動(dòng)態(tài)讀取外部文件全解析
- Unity各種路徑
- Unity官方文檔
本文作者: Sheh偉偉
本文鏈接: http://davidsheh.github.io/2017/02/09/Unity路徑的相關(guān)總結(jié)/
版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY-NC-SA 3.0 許可協(xié)議呼盆。轉(zhuǎn)載請(qǐng)注明出處年扩!