unity使用UnityWebRequest加载AssetBundle包

unity加载AssetBundle包并显示进度条

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ABTest : MonoBehaviour {

    public Text textProgress;
    private void Awake()
    {
        StartCoroutine(Download());
    }
    IEnumerator Download()
    {
        //ab包的地址
        string url = Application.streamingAssetsPath + "/scenes/"+ "test" + ".ab";  
        AssetBundle ab ;
        using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url))
        {
             //yield return request.SendWebRequest();
            request.SendWebRequest();
            while (!request.isDone)
            {
                Debug.Log(request.downloadProgress);
                //textProgress.text = (request.downloadProgress * 100).ToString("F0") + "%";
                yield return null;
            }
            if (request.isDone)
            {
                Debug.Log(100);
                //textProgress.text = 100 + "%";
            }
            ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
            if (ab == null)
            {
                Debug.LogError("AB包为空");
                yield break;
            }
        }
        //ab.Unload(false); 
        Debug.Log("成功");
        yield break;
    }
}

你可能感兴趣的:(unity,C#,AssetBundle)