Unity3d各平台資源路徑文件夾


之前一直是PC項目,公司終於考慮移動平台了,但是試驗了幾把,感覺移動平台資源管理路徑還是有很多隱藏的注意事項。

比如在PC上可以做到隨便讀寫,但是在移動平台就涉及到權限問題。

看到小伙伴的總結,還是要記錄一下,好記性不如爛筆頭啊,主題如下:

 

1.Unity的資源數據加載
2.Resource、StreamingAsset文件夾,安裝后的路徑(Android,iOS)
3.Unity在打包和安裝的時候怎么處理persistentDataPath
4.Unity的Android和IOS上相關的目錄結構
5.Unity常用目錄對應的Android && iOS平台地址

下面開始細說具體的注意事項>>>>>>>>>>>>

 

1.Unity的資源數據加載 - Resources、AssetBundle、StreamingAsset、PersistentDataPath
Resources目錄
- 打包集成到.asset文件里面及引用的資源as后se一個文件里面面
- 主線程加載
- 想要動態更新資源則不考慮
AssetBundle
- unity定義的二進制文件類型
- 用WWW類下載


StreamingAssets目錄
- 可讀不可寫
- 內容限制 - 無
- 只能用WWW類下載


PersistentDataPath目錄
- 可讀可寫
- 內容限制 - 無
- 清除手機緩存文件會一並清理這里的東西
- 隨意弄,可作為本地目錄讓WWW下載、也可以自己用FileInfo 或者FileStream讀寫;

    private void WriteCfg(string content)
    {
        string targetPath = Application.persistentDataPath + "/cfg.txt";
        StreamWriter sw;
        FileInfo t = new FileInfo(targetPath);
        if (!t.Exists)
        {
            //如果此文件不存在則創建  
            sw = t.CreateText();
        }
        else
        {
            //如果此文件存在則打開  
            sw = t.AppendText();
        }
        //以行的形式寫入信息  
        sw.WriteLine(content);
        sw.Close();
        sw.Dispose(); 
    }

 

                FileStream fs = new FileStream(targetPath, FileMode.Create, FileAccess.ReadWrite);
                fs.Write(www.bytes, 0, www.bytes.Length);
                fs.Flush();
                fs.Close();

 



2.Resource、StreamingAsset文件夾,安裝后的路徑(Android,iOS)

StreamingAsset

-PC:Application.dataPath + "/StreamingAssets";
- iOS : Application.dataPath + “/Raw”;
- Android : jar:file:// + Application.dataPath + !/assets/
Unity目前在Android平台也直接提供jar:file的格式,無需自己添加,直接使用:

只能通過www讀取,因為該文件夾在jar包內部,協議是jar:file:// ,Application.streamingAssetsPath返回的路徑會自動添加該協議。

 

public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
    public string result = "";
    IEnumerator Example() {
        if (filePath.Contains("://")) {
            WWW www = new WWW(filePath);
            yield return www;
            result = www.text;
        } else
            result = System.IO.File.ReadAllText(filePath);
    }

 

 



Resources
- 打包成一個Asset文件

3.Unity在打包和安裝的時候怎么處理PersistentDataPath
- PersistentDataPath- 就是com.**.**/files 的路徑而已
比如Android發布導出設置 write acess選擇SD Card,否則就失去了讀寫的意義,www讀取,stream讀寫。

注意,WWW加載時 Application.persistentDataPath返回的路徑不會自動添加協議,需要手動添加,協議是file://而不是jar:// 因為導出包是外面的SD卡。

該目錄會在SD卡中暴露出來,無需root權限,屬於本地文件,可以直接在手機文件管理中看到,因此也需要本地文件協議file://讀取,日志可以寫於此處。


4.Unity的Android和IOS上相關的目錄結構
Android:
- assets 游戲內容相關的都在這里了
- lib JNI相關的東西
- META-INF Java包跟rar包的區別
- res 圖標之類的
- AndroidManifest.xml Android配置文件
- classes.dex Java虛擬機runtime的東西
- resources.arsc Java編譯后的二進制文件

IOS:
- level0/level1… Scene
- sharedassets0/shaedassets1/… Scene相關的東西
- Managed 腳本編譯后的dll
- resources.assets Resources里面的東西
- Raw StreamingAssets里面的東西

5. Unity常用目錄對應的Android && iOS平台地址
IOS:
Application.dataPath : Application/xxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxx/Documents
Application.temporaryCachePath : Application/xxxxx/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

轉自Unity3D吧,以上內容僅做參考,最新使用規范參見官方地址:

http://docs.unity3d.com/ScriptReference/Application-dataPath.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM