U3D assetbundle加载

本文介绍Unity3D中AssetBundle的加载方法,通过使用WWW.LoadFromCacheOrDownload进行资源缓存或下载,并演示了如何加载AssetBundle并实例化其中的GameObject。此过程适用于从本地或网络加载资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

U3D assetbundle加载

 1 using UnityEngine;
 2 using System.Collections;
 3 public class testLoadFromAB : MonoBehaviour {
 4 
 5     IEnumerator DownloadAndCache()
 6     {
 7         while (!Caching.ready)
 8             yield return null;
 9 
10         //注意,从本地加载时,必须使用前缀 file:///或file://,从网络加载则使用 http://,这两种协议可以在iphone和WINDOWS, 安卓上通用
11         //UNITY MANUAL:
12         //http://, https:// and file:// protocols are supported on iPhone. 
13         //ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported.
14         WWW www = WWW.LoadFromCacheOrDownload ("file:///Z:/unity/learn-test/Assets/AssetBundles/cubes.unity3d", 22);
15         yield return www;
16         if(!string.IsNullOrEmpty (www.error)){//有些平台不支持string为null,这种写法可以避免意外
17             Debug.LogError (www.error);
18             yield break;
19         }
20 
21         AssetBundle bundle = www.assetBundle;
22 
23         //注意必须使用Instantiate实例化出来才能将两个CUBE显示到场景中
24         GameObject cube1 = Instantiate (bundle.LoadAsset ("DecalCube2")) as GameObject;
25         GameObject cube2 = Instantiate (bundle.LoadAsset ("DecalCube3")) as GameObject;
26 
27     }
28     // Use this for initialization
29     void Start () {
30         StartCoroutine ("DownloadAndCache");
31     }
32     
33     // Update is called once per frame
34     void Update () {
35     
36     }
37 }

 

posted on 2016-10-22 12:50 时空观察者9号 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值