移動平台路徑相關

來源:互聯網
上載者:User

標籤:本地   project   運行時   ram   動態   特殊   xxxxx   net   tco   

轉載自:http://blog.csdn.net/u014735301/article/details/43481727

移動平台的資源路徑問題

想要讀取一個檔案,自然首先要找到這個檔案,下面小匹夫首先會總結一下unity3d中存在的各個地址,之後再總結一下各個地址在各個移動平台中的對應位置。

Unity3D中的資源路徑
Application.dataPath 此屬性用於返回程式的資料檔案所在檔案夾的路徑。例如在Editor中就是Assets了。
Application.streamingAssetsPath 此屬性用於返迴流資料的緩衝目錄,返迴路徑為相對路徑,適合設定一些外部資料檔案的路徑。
Application.persistentDataPath 此屬性用於返回一個持久化資料存放區目錄的路徑,可以在此路徑下儲存一些持久化的資料檔案。
Application.temporaryCachePath 此屬性用於返回一個臨時資料的緩衝目錄。

 

  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

 

 

 

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

 

 

 

 

上面的3張表格,我們可以看到 dataPath和streamingAssetsPath的路徑位置一般是相對程式的安裝目錄位置,而persistentDataPath和temporaryCachePath的路徑位置一般是相對所在系統的固定位置。

簡單介紹一下unity3d中資源的處理種類(歡迎拍磚):

小匹夫遇到過的大體就是如下幾種了,Resources、StreamingAssets、AssetBundle、PersistentDataPath,下面簡單分析一下。

Resources:

是作為一個Unity3D的保留檔案夾出現的,也就是如果你建立的檔案夾的名字叫Resources,那麼裡面的內容在打包時都會被無條件的打到發布包中。它的特點簡單總結一下就是:

  1. 唯讀,即不能動態修改。所以想要動態更新的資源不要放在這裡。
  2. 會將檔案夾內的資源打包整合到.asset檔案裡面。因此建議可以放一些Prefab,因為Prefab在打包時會自動過濾掉不需要的資源,有利於減小資源套件的大小。
  3. 主線程載入。
  4. 資源讀取使用Resources.Load()。
StreamingAssets:

要說到StreamingAssets,其實和Resources還是蠻像的。同樣作為一個唯讀Unity3D的保留檔案夾出現。不過兩者也有很大的區別,那就是Resources檔案夾中的內容在打包時會被壓縮和加密。而StreamingAsset檔案夾中的內容則會原封不動的打入包中,因此StreamingAssets主要用來存放一些二進位檔案。下面也同樣做一個簡單的總結:

  1. 同樣,唯讀不可寫。
  2. 主要用來存放二進位檔案。
  3. 只能用過WWW類來讀取。
AssetBundle:

關於AssetBundle的介紹已經有很多了。簡而言之就是把prefab或者二進位檔案封裝成AssetBundle檔案(也是一種二進位)。但是也有硬傷,就是在移動端無法更新指令碼。下面簡單的總結下:

  1. 是Unity3D定義的一種二進位類型。
  2. 最好將prefab封裝成AseetBundle,不過上面不是才說了在移動端無法更新指令碼嗎?那從Assetbundle中拿到的Prefab上掛的指令碼是不是就無法運行了?也不一定,只要這個prefab上掛的是本地指令碼,就可以。
  3. 使用WWW類來下載。
PersistentDataPath:

看上去它只是個路徑呀,可為什麼要把它從路徑裡面單獨拿出來介紹呢?因為它的確蠻特殊的,這個路徑下是可讀寫。而且在IOS上就是應用程式的沙箱,但是在Android可以是程式的沙箱,也可以是sdcard。並且在Android打包的時候,ProjectSetting頁面有一個選項Write Access,可以設定它的路徑是沙箱還是sdcard。下面同樣簡單的總結一下:

  1. 內容可讀寫,不過只能運行時才能寫入或者讀取。提前將資料存入這個路徑是不可行的。
  2. 無內容限制。你可以從StreamingAsset中讀取二進位檔案或者從AssetBundle讀取檔案來寫入PersistentDataPath中。
  3. 寫下的檔案,可以在電腦上查看。同樣也可以清掉。

好啦,小匹夫介紹到這裡,各位看官們是不是也都清楚了一些呢?那麼下面我們就開始最後一步了,也就是如何在移動平台如何讀取外部檔案。

移動平台讀取外部檔案的方法

上文小匹夫之所以要介紹Resources、StreamingAssets、AssetBundle、PersistentDataPath這四個東東,就是因為讀取外部資源的操作所涉及到的東西無外乎這幾種。既然是用Unity3D來開發遊戲,那麼自然要使用Unity3D規定的操作方式,而不是我們在PC上很原始的那種操作方式來操作咯。否則就會像本文一開始所示範的那樣,寫出移動端無法使用的很傻的代碼來。

下面小匹夫就分別實現一下利用Resources、StreamingAssets、AssetBundle來讀取的過程。

 Resources:

首先我們建立一個Resources目錄,並且將上面我們用到的Test.xml複製一份到這個檔案夾中。

然後我們通過Resources的讀取方法來讀取Test.xml的內容。並且調用GUI將xml的內容繪製出來。

//用Resources讀取xmlusing UnityEngine;using System.Collections;using EggToolkit;using System.Xml.Linq;using System.Xml;public class Test : MonoBehaviour {    private string _result;    // Use this for initialization    void Start () {        LoadXML("Test");    }        // Update is called once per frame    void Update () {        }    private void LoadXML(string path)    {        _result = Resources.Load(path).ToString();        XmlDocument doc = new XmlDocument();        doc.LoadXml(_result);     }    void OnGUI()    {        GUIStyle titleStyle = new GUIStyle();          titleStyle.fontSize = 20;          titleStyle.normal.textColor = new Color(46f/256f, 163f/256f, 256f/256f, 256f/256f);          GUI.Label(new Rect(400, 10, 500, 200),  _result,titleStyle);    }}

結果

OK,Resources讀取外部資源目標達成!!

下面我們繼續,這次則是使用StreamingAssets來操作。

StreamingAssets:

同Resources一樣,我們要建立一個StreamingAssets的檔案夾來存放我們的Test.xml檔案。

不過前文已經說了,StreamingAssets檔案夾內的東西並不會被壓縮和加密,而是放進去什麼就是什麼,所以一般是要放二進位檔案的,這裡小匹夫僅僅做一個示範,各位在實際操作中切記不要直接把資料檔案放到這個目錄中打包。

 

using UnityEngine;using System.Collections;using EggToolkit;using System.Xml.Linq;using System.Xml;using System.IO;public class Test : MonoBehaviour {    private string _result;    // Use this for initialization    void Start () {        StartCoroutine(LoadXML());    }        // Update is called once per frame    void Update () {        }    /// <summary>    /// 如前文所述,streamingAssets只能使用www來讀取,    /// 如果不是使用www來讀取的同學,就不要問為啥讀不到streamingAssets下的內容了。    /// 這裡還可以使用persistenDataPath來儲存從streamingassets那裡讀到內容。    /// </summary>    IEnumerator LoadXML()    {        string sPath= Application.streamingAssetsPath + "/Test.xml";        WWW www = new WWW(sPath);        yield return www;        _result = www.text;    }        void OnGUI()    {        GUIStyle titleStyle = new GUIStyle();          titleStyle.fontSize = 20;          titleStyle.normal.textColor = new Color(46f/256f, 163f/256f, 256f/256f, 256f/256f);          GUI.Label(new Rect(400, 10, 500, 200),  _result,titleStyle);    }}

結果

OK,StreamingAssets讀取外部資源目標達成!!

下面我們繼續,最後則是使用AssetBundle來操作。

AssetBundle:

來到AssetBundle,這裡就和上面兩個不一樣了。首先我們要把我們的檔案Test.xml打成AssetBundle檔案,由於小匹夫使用的是小米3作為測試機,所以AssetBundle的平台選擇為Andorid。

,我們建立了一個AssetBundle檔案,並命名為TextXML。並且按照二進位檔案放入StreamingAssets檔案夾中的慣例,將這個AssetBundle檔案放入StreamingAssets檔案夾。

那麼下面就是從AssetBudle中讀取Test.xml的內容咯。直接上代碼:

//從AssetBundle中讀取xmlusing EggToolkit;using System.Xml.Linq;using System.Xml;using System.IO;public class Test : MonoBehaviour {    private string _result;        // Use this for initialization    void Start () {        LoadXML();    }        // Update is called once per frame    void Update () {            }    void LoadXML()    {        AssetBundle AssetBundleCsv = new AssetBundle();        //讀取放入StreamingAssets檔案夾中的bundle檔案        string str = Application.streamingAssetsPath + "/" + "TestXML.bundle";        WWW www = new WWW(str);        www = WWW.LoadFromCacheOrDownload(str, 0);            AssetBundleCsv = www.assetBundle;        string path = "Test";            TextAsset test = AssetBundleCsv.Load(path, typeof(TextAsset)) as TextAsset;        _result = test.ToString();    }        void OnGUI()    {        GUIStyle titleStyle = new GUIStyle();          titleStyle.fontSize = 20;          titleStyle.normal.textColor = new Color(46f/256f, 163f/256f, 256f/256f, 256f/256f);          GUI.Label(new Rect(400, 10, 500, 200),  _result,titleStyle);    }    }

結果

 

OK,AssetBundle讀取外部資源目標也達成了!!

 

補充:

在此統一回答一下在評論和qq上有同學提出的一個問題:安卓上Application.persistentDataPath的內容貌似不是匹夫你表裡的那個呀?在本文的評論裡小匹夫已經回複過了,其實文中也說過

    但是在Android可以是程式的沙箱,也可以是sdcard。並且在Android打包的時候,ProjectSetting頁面有一個選項Write Access,可以設定它的路徑是沙箱還是sdcard。

 

移動平台路徑相關

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.