- Home /
Pausing Until AssetBundle Downloads
We currently have a system where assets are loaded via ResourceController.INSTANCE.Load(assetName). There is a FileSystemResourceController, an AssetBundleResourceLoader and a UnityResourceLoader (derived from ResourceController)
These handle editor , standalone , web(scorm) asset loading differently. We are moving towards having all our lesson specific assets in their own bundles which means calls like ResourceController.INSTANCE.Load("level1config.xml") need to "wait" until the bundle that contains the asset is downloaded (in web player case). The code immediately following needs to have that file loaded to continue. Has anyone else solved something similar? (ie an AssetLoader whose purpose it is to return assets from bundles ...that might need to wait for bundles to be downloaded before proceeding?) thanks, --matt
do you $$anonymous$$d posting your code for your derived class so that we can deter$$anonymous$$e a good place to pause at?
Answer by cregox · Jun 11, 2013 at 10:54 PM
Kinda late reply, and I'm not sure what you're asking, but I do a lot of work with bundles and yours seems like a pretty straight-forward question. If I'm right, this should have helped ya if posted early enough:
Usage
static public IEnumerator CoDownload () {
FileHandler fileHandler = new FileHandler();
IEnumerator e = fileHandler.Download("http://www.example.com/index.html");
while ( e.MoveNext() )
yield return e.Current;
if ( fileHandler.DownloadOk() )
fileHandler.Save("somewhere/somefile.html");
else
Debug.LogError("error on download: " + fileHandler.www.error);
}
FileHandler.cs
using System.IO;
using UnityEngine;
using System.Collections;
///
/// File handler, wait for file download and do the saving process.
///
public class FileHandler {
public WWW www { get; protected internal set; }
public IEnumerator Download (string url) {
www = new WWW(url); // initiate download
yield return www;
}
public bool Save (string into) {
bool savedOk = false;
if (www != null && www.isDone && www.error == null) {
FileStream stream = null;
try {
stream = new FileStream(
into
, FileMode.Create);
} catch (System.SystemException exception) {
Debug.LogError("[FileHandler] FileStream error, which should never happen: "+ exception.Message);
}
if (stream != null) {
stream.Write(www.bytes, 0, www.bytes.Length);
stream.Close();
savedOk = true;
} else {
Debug.LogError("[FileHandler] Couldn't write to file because stream is null: "+ into);
if (www == null || www.error == null) {
Debug.LogWarning("[FileHandler] www or error is null - *most likely* the file to download was changed and this is not being loaded any more: "+ into);
} else {
Debug.LogError("[FileHandler] Couldn't save file!! "+ into +" error: "+ www.error +" error length? "+ www.error.Length);
}
}
}
Dispose();
return savedOk;
}
public void Dispose () {
www.Dispose();
www = null;
}
public bool DownloadOk () {
if (www == null) {
return false;
} else if (www.error != null) {
return false;
}
return true;
}
}
Answer by dryheat70 · May 02, 2012 at 04:46 PM
I'm afraid that would be a lot of code. The key hangup, however, is that we have code throughout similar to
levelConfigXML = ResourceController.INSTANCE.Load("level1config.xml");
// parse levelConfigXML and assume it's already been loaded
// Below is pseudo code
class AssetBundleResourceLoader : IResourceController
{
WWW bundleLoader;
String CurrBundleName;
Object Load(string path)
{
String bundleName = GetBundleFromPath(path);
if(bundleName != CurrBundleName)
{
bundleLoader = RequestController.RequestAndWaitForLoad(CurrBundleName);
}
Object result = null;
if(bundleLoader != null && bundleLoader.error == null && bundleLoader.isDone)
{
if(bundleLoader.assetBundle != null)
{
result = bundleLoader.assetBundle.Load(path);
}
}
if(result == null)
{
// fallback to "parent" loader
result = base.Load(path);
}
return result;
}
class WWWRequestController
{
WWW RequestAndWaitForLoad(string url)
{
// have made many attempts here to essentially start a d/l of bundle and "wait" for it to complete. How have others solved this?
}
}
{
}
Answer by dryheat70 · May 02, 2012 at 09:46 PM
found a good link to a page explaining walking through an IEnumerable yourself Unity3D coroutines in detail
that I thought might help, but haven't had much luck... still loops through with WWW.isDone = false and no error in WWW.error.
but, wasn't able to get it to effectively block until download was complete with the following code...
public WWW RequestAndWaitForLoad (string url)
{
isLoadingBundle = true;
Debug.Log(String.Format("##WWW requesting {0}", url));
int test = 0;
IEnumerator e = FinishDownload(url);
while(e.MoveNext())
{
Debug.Log(String.Format("##count:{0}", test++));
}
Debug.Log(String.Format("##RequestAndWaitForLoad after startcorouting "));
Debug.Log(String.Format("##isLoadingBundle is {0}", isLoadingBundle));
Debug.Log(String.Format("##wwwResults is {0} null", wwwResults == null ? "" : "NOT"));
return wwwResults;
}
protected IEnumerator FinishDownload(string url)
{
WWW theWWW = new WWW(url);
if(theWWW.error != null)
{
Debug.Log(String.Format("##FinishDownload breaking with error : {0}", theWWW.error));
yield break;
}
yield return theWWW;
Debug.Log(String.Format("##FinishDownload Ater first yield isDone is : {0}", theWWW.isDone));
if(theWWW.error != null)
{
Debug.Log(String.Format("##FinishDownload Ater first yield isDone is : {0}", theWWW.isDone));
}
wwwResults = theWWW;
yield break;
}
Your answer
Follow this Question
Related Questions
3D model is not loading from url 1 Answer
Unload asset bundle make problem 0 Answers
asset bundles download problem 0 Answers
WWW::Dispose doesn't work 1 Answer
How to Load all textures with WWW class? But not with Assets Bundles 1 Answer