U3D assetbundle加载与卸载的深入理解
生活随笔
收集整理的這篇文章主要介紹了
U3D assetbundle加载与卸载的深入理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
U3D assetbundle加載與卸載的深入理解
using UnityEngine; using System.Collections; using System;public class testLoadFromAB : MonoBehaviour {IEnumerator DownloadAndCache(){while (!Caching.ready)yield return null;//注意,從本地加載時,必須使用前綴 file:///或file://,從網絡加載則使用 http://,這兩種協議可以在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)){//有些平臺不支持string為null,這種寫法可以避免意外 // Debug.LogError (www.error); // yield break; // }//AssetBundle bundle = www.assetBundle;//注意必須使用Instantiate實例化出來才能將兩個CUBE顯示到場景中//Instantiate實際上復制,淺復制,destroy也是淺銷毀,只銷毀物體本身不管物體的引用。//prefab與gameobject一樣,有些組件是引用的,比如材質貼圖。//loadasset去加載一個prefab時,會把此預設用到的所有資源都加載到內存,比如材質貼圖。Instantiate (bundle.LoadAsset ("DecalCube2"));Instantiate (bundle.LoadAsset ("DecalCube3")) ;//參數為true時,將銷毀所有從loadasset加載出來的資源,比如DecalCube2.prefab的材質與貼圖。//雖然我們使用了Instantiate對DecalCube2.prefab進行了復制,但只是淺復制,當bundle.Unload (true)時//貼圖被釋放,我們的prefab就會丟失貼圖變為紫色。bundle.Unload (true);}// Use this for initializationvoid Start () {StartCoroutine ("DownloadAndCache");}// Update is called once per framevoid Update () {} }?
posted on 2016-11-01 16:46 時空觀察者9號 閱讀(...) 評論(...) 編輯 收藏
總結
以上是生活随笔為你收集整理的U3D assetbundle加载与卸载的深入理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LUA 协程
- 下一篇: 【转】 不适用Sqrt函数开方,精度小于