IOS:
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
Android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
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
Windows Web Player:
Application.dataPath : file:///D:/MyGame/WebPlayer (即導包后保存的文件夾,html文件所在文件夾)
Application.streamingAssetsPath :
Application.persistentDataPath :
Application.temporaryCachePath :
------------------------------------------------------------------------------------------------------------------------------
1.Resources文件夾
Resources文件夾是一個只讀的文件夾,通過Resources.Load()來讀取對象。因為這個文件夾下的所有資源都可以運行時來加載,所以Resources文件夾下的所有東西都會被無條件的打到發布包中。建議這個文件夾下只放Prefab或者一些Object對象,因為Prefab會自動過濾掉對象上不需要的資源。舉個例子我把模型文件還有貼圖文件都放在了Resources文件夾下,但是我有兩張貼圖是沒有在模型上用的,那么此時這兩張沒用的貼圖也會被打包到發布包中。假如這里我用Prefab,那么Prefab會自動過濾到這兩張不被用的貼圖,這樣發布包就會小一些了。
2.StreamingAssets
StreamingAssets文件夾也是一個只讀的文件夾,但是它和Resources有點區別,Resources文件夾下的資源會進行一次壓縮,而且也會加密,不使用點特殊辦法是拿不到原始資源的。但是StreamingAssets文件夾就不一樣了,它下面的所有資源不會被加密,然后是原封不動的打包到發布包中,這樣很容易就拿到里面的文件。所以StreamingAssets適合放一些二進制文件,而Resources更適合放一些GameObject和Object文件。 StreamingAssets 只能用過www類來讀取!!
------------------------------------------------------------------------------------------------------------------------------
StreamingAssets,在不同的平台上面 (Windows, Ios ,Android),該目錄最終發布的位置不同,所以讀取的方法也不同。
WWW是異步加載所以執行加載命令式不能直接執行讀取解析操作,
要等待
WWW www = new WWW(filePath);
yield return www; // while (!www.isDone) {}
result = www.text;
1.把你要讀取的文件放在Unity項目的Assets/StreamingAssets文件夾下面,沒有這個文件夾的話自己建一個。
2.讀取的代碼(假設名為"文件.txt")

byte[] InBytes; //用來存儲讀入的數據 if (Application.platform == RuntimePlatform.Android) //判斷當前程序是否運行在安卓下 { string FileName = "jar:file://" + Application.dataPath + "!/assets/" + "文件.txt"; WWW www = new WWW(FileName); //WWW會自動開始讀取文件 while(!www.isDone){} //WWW是異步讀取,所以要用循環來等待 InBytes = www.bytes; //存到字節數組里 } else { //其他平台的讀取代碼 }
根目錄:StreamingAssets文件夾
#if UNITY_EDITOR
string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml";
#elif UNITY_IPHONE
string filepath = Application.dataPath +"/Raw"+"/my.xml";
#elif UNITY_ANDROID
string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
#endif
根目錄:Resources 文件夾
可以使用Resources.Load("名字"); 把文件夾中的對象加載出來
根目錄:Application.dataPath 文件夾
可以使用Application.dataPath進行讀操作
Application.dataPath: 只可讀不可寫,放置一些資源數據
Application.persistentDataPath
IOS與android平台都可以使用這個目錄下進行讀寫操作,可以存放各種配置文件進行修改之類的。
在PC上的地址是:C:\Users\用戶名 \AppData\LocalLow\DefaultCompany\test
------------------------------------------------------------------------------------------------------------------------------總結:
一.在項目根目錄中創建Resources文件夾來保存文件。
可以使用Resources.Load("文件名字,注:不包括文件后綴名");把文件夾中的對象加載出來。
注:此方可實現對文件實施“增刪查改”等操作,但打包后不可以更改了。
二.直接放在項目根路徑下來保存文件
在直接使用Application.dataPath來讀取文件進行操作。
注:移動端是沒有訪問權限的。
三.在項目根目錄中創建StreamingAssets文件夾來保存文件。
1.可使用Application.dataPath來讀取文件進行操作。
#if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml"; #elif UNITY_IPHONE string filepath = Application.dataPath +"/Raw"+"/my.xml"; #elif UNITY_ANDROID string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml; #endif
2.直接使用Application.streamingAssetsPath來讀取文件進行操作。
注:此方法在pc/Mac電腦中可實現對文件實施“增刪查改”等操作,但在移動端只支持讀取操作。
四.使用Application.persistentDataPath來操作文件(薦)
該文件存在手機沙盒中,因為不能直接存放文件,
1.通過服務器直接下載保存到該位置,也可以通過Md5碼比對下載更新新的資源
2.沒有服務器的,只有間接通過文件流的方式從本地讀取並寫入Application.persistentDataPath文件下,然后再通過Application.persistentDataPath來讀取操作。