Unity3D資源目錄及資源讀取


一、各平台上的資源目錄

1.在Unity3D中的目錄:

Application.dataPath 此屬性用於返回程序的數據文件所在文件夾的路徑。例如在Editor中就是Assets了。
Application.streamingAssetsPath 此屬性用於返回流數據的緩存目錄,返回路徑為相對路徑,適合設置一些外部數據文件的路徑。
Application.persistentDataPath 此屬性用於返回一個持久化數據存儲目錄的路徑,可以在此路徑下存儲一些持久化的數據文件。
Application.temporaryCachePath 此屬性用於返回一個臨時數據的緩存目錄。

2.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

3.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的路徑位置一般是相對所在系統的固定位置。

二、資源處理類型

Unity中我們使用到的資源類型主要有: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. 無內容限制。你可以從 StreamingAssets 中讀取二進制文件或者從 AssetBundle讀取文件來寫入 PersistentDataPath 中。
  3. 寫下的文件,可以在電腦上查看。同樣也可以清掉。

案例:

1.Resources:

_result = Resources.Load(path).ToString();

2.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);
  }
}

3.AssetBundle:

//從AssetBundle中讀取xml
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 () {
    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);
  }
}

  


免責聲明!

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



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