转自:原文地址
一、各平台上的资源目录
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,那么里面的内容在打包时都会被无条件的打到发布包中。它的特点简单总结一下就是:
要说到StreamingAssets,其实和Resources还是蛮像的。同样作为一个只读的Unity3D的保留文件夹出现。不过两者也有很大的区别,那就是Resources文件夹中的内容在打包时会被压缩和加密。而StreamingAsset文件夹中的内容则会原封不动的打入包中,因此StreamingAssets主要用来存放一些二进制文件。下面也同样做一个简单的总结:
AssetBundle:
关于AssetBundle的介绍已经有很多了。简而言之就是把prefab或者二进制文件封装成AssetBundle文件(也是一种二进制)。但是也有硬伤,就是在移动端无法更新脚本。下面简单的总结下:
PersistentDataPath:
看上去它只是个路径呀,可为什么要把它从路径里面单独拿出来介绍呢?因为它的确蛮特殊的,这个路径下是可读写。而且在IOS上就是应用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard。并且在Android打包的时候,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard。下面同样简单的总结一下:
_result = Resources.Load(path).ToString();
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); } }