Unity——assetbundle下載

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestLoadAssetbundle : MonoBehaviour {
    //網絡地址直接就是url  本地地址需要在前面加  file://   例如 file://D:\1\cube 1.s  (.s是我自定義的assetbundle後綴名,可以自己修改)
    [SerializeField]
    private string url; 
    //此處爲預設的名字
    [SerializeField]
    private string assetname;
	void Start () {
        //在Start方法裏開啓協程
        StartCoroutine(LoadAssetbundleMethod());
	}
    /// <summary>
    /// 加載assetbundle的協程
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadAssetbundleMethod()   
    {
        using (WWW www = new WWW(url))    //using釋放資源 
        {
            yield return www;             //等待www下載完成
            if (www.error != null)        //判斷是否有錯
            {
                Debug.LogError("網絡錯誤");
            }
            else
            {
                AssetBundle bundle = www.assetBundle;
                Object obj = bundle.LoadAsset(assetname); //根據prefab的名字下載
                Instantiate(obj);
                bundle.Unload(false);     //釋放bundle  true:加載的全部卸載  false:用過的卸載掉
            }
        }
    }
}

本文僅作個人學習筆記,方便複習使用

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章