Synchronously loading a texture from an asset bundle

Hi! I used to have some code that loaded textures from the Resources file. Something like this:

myTexture = Resources.Load(pathToTexture) as Texture;

Unfortunately, the file has grown so big that I was forced to move the textures into a group of asset bundles. The problem I’m facing now is that, before attempting to load a texture from an asset bundle, I need to download the asset bundle first using LoadFromCacheOrDownload. However, the loading process for asset bundles is asynchronous and needs to be done inside a coroutine, and my game is structured in such a way that I need to grab the texture when I need it as a single step. Is there any way to be able to load the texture from the an asset bundle in such a way that the above code can be replaced by something like this?

myTexture = SomeWrapperClass.LoadTextureFromAssetBundle(pathToTexture);

Thanks in advance for your help!

Hi Nacho !

The coroutine used to download the assetbundle and to load a file from it is necessary to avoid your game freezing during this process.
The best practice is to start your asset bundle download and your texture loading in a coroutine while showing a temporary texture or a loading panel to the player. Then, call a function when the download is finished to continue your normal program flow.

But, if for any reason you want to stop your application until the download is finished, you can call LoadFromCacheOrDownload in a normal method, then wait for the WWW to end the download, and continue your process :

WWW www = LoadFromCacheOrDownload("http://my.asset.bundle/path");
while(!www.isDone);
if(!string.isNullOrEmpty(www.error))
{
    // load texture here with www.assetbundle.Load
}
else
{
    Debug.LogError("Unable to open the asset bundle : " + www.error)
}

The problem with this way of using the WWW class is that if the download take 60 seconds to finished, the entire program will be stuck on the while(!www.isDone); and the player will have no other control on your application (for exemple, Apple certification could reject an application that freeze more than 15 seconds).