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 }
时间: 2024-08-25 16:00:42

U3D assetbundle加载的相关文章

关于unity 中使用AssetBundle加载资源,shader偶尔会丢失的问题解决办法

问题描述: 因为项目中要进行热更新设计,所以用unity官方推荐的打包方式assetbundle进行打包,打包好了以后再电脑上运行正常,但是当发布到android上后发现偶尔场景背景会出现空白,多方查找,发现unity存在使用assetbundle加载资源会丢失shader的问题.网上找了一些解决方式,测试可用,分享出来. 方案一:(亲测可用) 第一步,在将用到的Shader加到Editor->Graphics Settings的Shader列表里再进行打包(依赖打包) 第二步,在代码中给sha

AssetBundle加载API

AssetBundle加载API 在Unity 5当中,可以通过4个不同的API来加载AssetBundle,4个API可以用两个条件来区分: AssetBundle是 LZMA压缩. LZ4压缩还是不压缩的 加载AssetBundle的平台 这4个API分别是: AssetBundle.LoadFromMemoryAsync AssetBundle.LoadFromFile WWW.LoadFromCacheOrDownload UnityWebRequest 的 DownloadHandle

WebGL AssetBundle 加载 缓存

U3D的AssetBundle真的是博(坑)大精(坑)深啊 安卓的话要先打包到StreamingAssetPath中,安装后第一次运行,自动把Streaming 中的解压到PersistentDataPath中,因为persistent目录是在运行过一次才创建的. 这样,之后再更新资源就直接下载到persistent目录下了 但是,WebGL项目呢 这个就不存在什么streaming或者persistent了,因为不能加载本地路径 所以就只有缓存,加载包时就要用 WWW.LoadFromDown

Unity5.x版本AssetBundle加载研究

之前说了 “Unity5.x版本AssetBundle打包研究”,没看过的请先看一下:http://www.shihuanjue.com/?p=57再来看本文,有一定的连接性. 先梳理一下思路:要加载一个资源A,必须先去加载它的所有依赖资源要知道这个资源A依赖了哪些资源,必须先去加载AssetBundleManifest通过AssetBundleManifest对象的GetAllDependencies(A)方法,获取它依赖的所有资源.依赖资源都加载了,就可以去真正加载资源A了. 注意点:1.资

u3d 异步加载场景以及进度条的显示

1.先建立两个场景 2.把两个场景在在build setting 中添加两个建好的两个场景 3.在第一个场景中建立一个button和slider组件 4.代码处理 using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.SceneManagement;public class ButtonTest : MonoBehaviour { public Button ButtonTestTran

u3d外部资源 打包与加载的问题

被坑了一下午,调bug,u3d外部加载资源一会可以,一会不行,始终找不到问题,最后快下班的时候,重新试了一下,原来是资源打包之前的文件名,和之后的加载资源名必须一样 [MenuItem("Custom Editor/Build AssetBundle From Selection Twice")] static void ExportResourceNoTrack() { // Bring up save panel string path = EditorUtility.SaveFi

Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用

下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确,是可以保证资源完全没有泄露的. using UnityEngine; using System.Collections; public class TestAssetBundle : MonoBehaviour { public string AssetBundleName = "cube1.ass

Unity的资源加载以及AssetBundle的一些坑

AssetBundle加载完毕,进行其中Asset的初始化后,不能立即Unload().否则Asset的初始化会有问题,依赖关系会Missing.需要等待个1,2秒再Unload().因为Instantiate()也有一个delay,尽管它可以立即返回一个GameObject对象. Static Batch只对预先在场景中摆好的静态对象起作用.运行时加载的对象则可能会失效.这时可以调用StaticBatchingUtility.Combine(GameObject root);手动触发Stati

Unity3d 5.x AssetBundle打包与加载

1.AssetBundle打包 unity 5.x版本AssetBundle打包,只需要设置好AssetBundle的名称后,unity会自动将其打包,无需处理其他,唯独需要做的是设置好个AssetBundle的名称. 注意:AssetBunlde的名称只能设置小写字母,即使你写成大写也会被自动转置成大写字母,而且名称中支持"/",如:"AssetBundles/cube.unity3d",.unity3d的后缀是自己设置的,可以不设置 代码: using Unit