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;
}
}