Hello all,
So I have a few assets bundled up, mind you there are no prefabs, just files from the Resources.Load folder and the StreamedAssets Folder.
My file is quite big and I would like to see the progress bar loading but it’s not working…
Also will I be able to just use Resources.Load etc after they are uncompressed?
private IEnumerator Download()
{
WWW download = WWW.LoadFromCacheOrDownload( "http://www.server.com/whatever.unity3d", 1 );
while( download.progress < 1 )
{
m_CurrentValue = download.progress * 100;
Debug.Log( string.Format( "Progress - {0}%. from {1}", download.progress * 100, download.url ) );
yield return new WaitForSeconds( .1f );
}
yield return download;
if( download.error != null )
{
Debug.LogError( download.error );
return false;
}
AssetBundle bundle = download.assetBundle;
bundle.LoadAll();
}
I think you should remove the yield for “download” and do something like this:
WWW download = WWW.LoadFromCacheOrDownload( "http://www.server.com/whatever.unity3d", 1 );
while( !download.isDone ) {
m_CurrentValue = download.progress * 100;
yield return null;
}
if (!string.IsNullOrEmpty(download.error)) {
// error!
} else {
// success!
}
1 Like
Woo it works by removing the yield return download… But why? I left it with yield return new WaitForSeconds( .1f ); so I can see the bar loading
1 Like
Why don’t you start another co-routine for the progress? I’m using Unity Web Request instead of WWW. Here is my solution.
private Transform loaderParent;
private bool isDownload = false;
public void LoadAssetBundle(string url, string bundleName)
{
StartCoroutine(AssetLoad(url, bundleName));
}
IEnumerator AssetLoad(string url, string bundleName)
{
using (UnityWebRequest request = UnityWebRequest.GetAssetBundle(url))
{
Debug.Log("Trying the bundle download");
isDownload = true;
StartCoroutine(progress(request));
yield return request.SendWebRequest();
isDownload = false;
if(request.isNetworkError || request.isHttpError)
{
Debug.Log("Error loading");
}
else
{
Debug.Log("Bundle downloaded");
//save the asset bundle
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
//test
//instantiate the prefab
GameObject obj = bundle.LoadAsset<GameObject>(bundleName);
Instantiate(obj, Vector3.zero, Quaternion.identity, loaderParent);
}
}
yield return null;
}
IEnumerator progress(UnityWebRequest req)
{
while (isDownload)
{
Debug.Log("Downloading : " + req.downloadProgress * 100 + "%");
yield return new WaitForSeconds(0.1f);
}
}
7 Likes