Unity资源加载路径及加载方式小结

Unity3D中的资源路径

路径属性 路径说明
Application.dataPath 此属性用于返回程序的数据文件所在文件夹的路径。例如在Editor中就是Assets了。
Application.streamingAssetsPath 此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。放在Unity工程StreamingAssets文件夹中的资源发布后都可以通过这个路径读取出来。
Application.persistentDataPath 此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。
Application.temporaryCachePath 此属性用于返回一个临时数据的缓存目录。

Unity3D中的资源的处理种类

Unity中的资源资源的处理种类大致分为:Resources、StreamingAssets、AssetBundle、PersistentDataPath 四类。

Resources

是作为一个Unity的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中。

特点:

  1. 只读,即不能动态修改。所以想要动态更新的资源不要放在这里。
  2. 会将文件夹内的资源打包集成到.asset文件里面。因此建议可以放一些Prefab,因为Prefab在打包时会自动过滤掉不需要的资源,有利于减小资源包的大小。
  3. 资源读取使用Resources.Load()。

StreamingAssets

StreamingAssets和Resources很像。同样作为一个只读的Unity3D的保留文件夹出现。不过两者也有很大的区别,那就是Resources文件夹中的内容在打包时会被压缩和加密。而StreamingAsset文件夹中的内容则会原封不动的打入包中,因此StreamingAssets主要用来存放一些二进制文件。

特点:

  1. 只读不可写。
  2. 主要用来存放二进制文件。
  3. 只能用过WWW类来读取。

AssetBundle

AssetBundle就是把prefab或者二进制文件封装成AssetBundle文件。

特点:

  1. 是Unity3D定义的一种二进制类型。
  2. 使用WWW类来下载。

PersistentDataPath

这个路径下是可读写。而且在IOS上就是应用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard。并且在Android打包的时候,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard。

  1. 内容可读写,不过只能运行时才能写入或者读取。 提前将数据存入这个路径是不可行的
  2. 无内容限制。你可以从 StreamingAsset 中读取二进制文件或者从 AssetBundle 读取文件来写入 PersistentDataPath 中。
  3. 写下的文件,可以在电脑上查看。同样也可以清掉。
  4. 需要使用WWW类来读取。

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

资源文件的读取

接下来我们来介绍一下Resources、StreamingAssets、AssetBundle、PersistentDataPath这四个东东的读取方法。

Resources

首先我们新建一个Resources目录,并且并将资源放在这目录中。如图:

https url

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoadResources : MonoBehaviour {

	public Image image;

	// Use this for initialization
	void Start () {
	
		image.overrideSprite = Resources.Load ("animotiong_2", typeof(Sprite)) as Sprite;
	
	}

}

StreamingAssets

首先我们新建一个StreamingAssets目录,并且并将资源放在这目录中。如图:

https url

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;
using System.Collections;

public class LoadResources : MonoBehaviour {

	string _result;


	// Use this for initialization
	void Start () {
	
		StartCoroutine(LoadXML());
	
	}
	
	IEnumerator LoadXML() {
		string sPath= Application.streamingAssetsPath + "/test.xml";
		WWW www = new WWW(sPath);
		yield return www;
		_result = www.text;
	}
}

PersistentDataPath

之前我们说过,内容可读写,不过只能运行时才能写入或者读取。 提前将数据存入这个路径是不可行的。也就是说,PersistentDataPath是在运行时生成的,例如通过网络下载资源存在放PersistentDataPath中。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;

public class LoadResources : MonoBehaviour {

	string _result;

	// Use this for initialization
	void Start () {
	
		StartCoroutine(LoadXML());
	
	}
	
	IEnumerator LoadXML() {
		string sPath= Application.persistentDataPath + "/test.xml";
		sPath = "file://" + sPath;
		WWW www = new WWW(sPath);
		yield return www;
		_result = www.text;

	}
}

这加载方式看起来与StreamingAssets很相识,但是注意这里多了行sPath = "file://" + sPath;这很重要!!想要通过WWW类加载PersistentDataPath必须使用file://协议实现加载。

猜你喜欢

转载自blog.csdn.net/hbysywl/article/details/80567289