U3D assetbundle加载与卸载的深入理解
生活随笔
收集整理的這篇文章主要介紹了
U3D assetbundle加载与卸载的深入理解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
U3D assetbundle加載與卸載的深入理解
using UnityEngine; using System.Collections; using System;public class testLoadFromAB : MonoBehaviour {IEnumerator DownloadAndCache(){while (!Caching.ready)yield return null;//注意,從本地加載時(shí),必須使用前綴 file:///或file://,從網(wǎng)絡(luò)加載則使用 http://,這兩種協(xié)議可以在iphone和WINDOWS, 安卓上通用//UNITY MANUAL://http://, https:// and file:// protocols are supported on iPhone. //ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported.//WWW www = WWW.LoadFromCacheOrDownload ("file:///Z:/unity/learn-test/Assets/AssetBundles/cubes.unity3d", 22); Debug.Log (Application.dataPath);Debug.Log (Application.streamingAssetsPath);Debug.Log (Application.persistentDataPath);Debug.Log (Application.temporaryCachePath);AssetBundle bundle = AssetBundle.LoadFromFile ("Assets/AssetBundles/cubes.unity3d"); // yield return www; // if(!string.IsNullOrEmpty (www.error)){//有些平臺(tái)不支持string為null,這種寫法可以避免意外 // Debug.LogError (www.error); // yield break; // }//AssetBundle bundle = www.assetBundle;//注意必須使用Instantiate實(shí)例化出來(lái)才能將兩個(gè)CUBE顯示到場(chǎng)景中//Instantiate實(shí)際上復(fù)制,淺復(fù)制,destroy也是淺銷毀,只銷毀物體本身不管物體的引用。//prefab與gameobject一樣,有些組件是引用的,比如材質(zhì)貼圖。//loadasset去加載一個(gè)prefab時(shí),會(huì)把此預(yù)設(shè)用到的所有資源都加載到內(nèi)存,比如材質(zhì)貼圖。Instantiate (bundle.LoadAsset ("DecalCube2"));Instantiate (bundle.LoadAsset ("DecalCube3")) ;//參數(shù)為true時(shí),將銷毀所有從loadasset加載出來(lái)的資源,比如DecalCube2.prefab的材質(zhì)與貼圖。//雖然我們使用了Instantiate對(duì)DecalCube2.prefab進(jìn)行了復(fù)制,但只是淺復(fù)制,當(dāng)bundle.Unload (true)時(shí)//貼圖被釋放,我們的prefab就會(huì)丟失貼圖變?yōu)樽仙?/span>bundle.Unload (true);}// Use this for initializationvoid Start () {StartCoroutine ("DownloadAndCache");}// Update is called once per framevoid Update () {} }?
posted on 2016-11-01 16:46 時(shí)空觀察者9號(hào) 閱讀(...) 評(píng)論(...) 編輯 收藏
總結(jié)
以上是生活随笔為你收集整理的U3D assetbundle加载与卸载的深入理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LUA 协程
- 下一篇: 【转】 不适用Sqrt函数开方,精度小于