admin 管理员组

文章数量: 1086019


2024年6月18日发(作者:源代码结局被入侵的人)

Unity3D游戏开发之加载和卸载资源包(AssetBundle)中的对象

Loading resources from AssetBundles

加载和卸载资源包 (AssetBundle) 中的对象

使用下载的数据构建资源包 (AssetBundle) 对象后,可以使用三种不同的方法加载

其中包含的对象:

• 会将其名称标识符用作参数加载对象。其名称即工程 (Project)

视图中显示的名称。可选择将对象类型作为参数传递到 Load 类函数,确保加载的是特定

类型的对象。

• ync 的作用原理与上述 Load 类函数相同,但不会在加载资源

后阻塞主线程。此方法对于加载较大资源或一次加载多个资源很有用,可避免应用程序停

止运行。

• l 将加载资源包 (AssetBundle) 中的所有对象。和

一样,可以按照其类型选择性地过滤对象。

要卸载资源,需要使用 。这个类函数包含一个布尔参数,可告

诉 Unity 是卸载所有数据(包括已加载的资源对象),还是只卸载已下载资源包中的压缩

数据。如果应用程序正在使用此资源包 (AssetBundle) 中的一些对象,并且需要释放一些

内存,则可传递 false 以便卸载内存中的已压缩数据。如需完全卸载该资源包

(AssetBundle) 的所有对象,则应传递 true,以便销毁从资源包加载的资源 (Assets)。

文章出处【狗刨学习网】

从资源包 (AssetBundles) 异步加载对象

可使用 ync 类函数异步加载对象,从而降低应用程序暂时中断

的可能性。

1. using UnityEngine;

2. // Note:This example does not check for look at the

example in the DownloadingAssetBundles section for more information

3. IEnumerator Start () {

4. // Start a download of the given URL

5. WWW www = omCacheOrDownload (url, 1);

6. // Wait for download to complete

7. yield return www;

8. // Load and retrieve the AssetBundle

9. AssetBundle bundle = undle;

10. // Load the object asynchronously

11. AssetBundleRequest request = ync ("myObject",

typeof(GameObject));

12. // Wait for completion

13. yield return request;

14. // Get the reference to the loaded object

15. GameObject obj = as GameObject;

16. // Unload the AssetBundles compressed contents to conserve memory

17. (false);

18. }

文章出处【狗刨学习网】


本文标签: 加载 资源 对象 卸载