I dabbled with Asset Bundles in Unity about a year ago and was disappointed to find that it introduced a lot of crashes and mem leaks into my game. Ultimately, I wasn’t able to use it for this reason. I recently say a video from Unite 2012 ( http://video.unity3d.com/video/6944412/unite-2012-asset-bundles ) and I figured now it was safe enough to go back to Asset Bundles. I even downloaded their tool for managing multiple asset bundles.
My problem now is that asset bundles downloaded via WWW.LoadFromCacheOrDownload (url, version) return an Asset Bundle object with a null .mainAsset property. Loading the prefab that it’s based on using Resources.Load seems to be just fine, however using WWW to fetch a compressed prefab returns null .mainAssets (regardless of .unity3d stored locally or remotely).
I don’t think this is a classic “Did you drag the prefab to the variable” since the asset bundles aren’t supposed to be available at build-time and are instead dynamically fetched at runtime. I’m not sure what’s going on though.
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
string url = "http://www.example.com/ab/chars/" + charName + ".unity3d";
Debug.Log ("Url-d: " + url);
using(WWW www = WWW.LoadFromCacheOrDownload (url, version)){
yield return www;
if (www.error != null)
{
//throw new Exception("WWW download had an error:" + www.error);
Debug.LogError("WWW download (" + url + ") had an error:" + www.error);
}
if(www == null)
{
Debug.LogError ("www object is null!");
}
AssetBundle bundle = www.assetBundle;
if(bundle == null)
{
Main.LogMessage ("[error] bundle is null!");
Debug.LogError ("null bundle");
}
GameObject newObj = null;
if(bundle.mainAsset == null)
{
Debug.LogError ("Main asset in url (" + url + ") is null");
}
try
{
newObj = Instantiate(bundle.mainAsset) as GameObject;
if(newObj == null)
{
Debug.Log ("Could not instantiate main asset in asset bundle.");
}
}
catch(System.Exception ex)
{
Debug.LogError(ex.Message);
}
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
}
Could do with also seeing the code that builds the bundles...
– whydoidoitDo you still have the prefab / assets in your project? Because i think it could be an AssetID problem. If the asset is already there the assetbundle might not be loaded. (pretty much the same what happens when you import a unityPackage into a project when certain or all asseta are already in the project.) However i don't use AssetBundles and haven't tested all aspects of them yet ;)
– Bunny83Thanks for the feedback guys. A few additional details: (1) The code that I used for the asset bundle building is available here http://twistedoakstudios.com/blog/Post74_asset-bundle-helper (2) I had the prefab in the project while running it in the editor, but I didn't want to include it in the version I built to iPhone/iPad because I wanted it to be loaded via asset bundle and save on initial download size
– jasonFHHSo that code doesn't set a main asset in the Asset Bundle - hence you don't find one...
– whydoidoit@whydoidoit ---> Omg. I think you're right. I wrongfully assumed any properly created asset bundle has a .mainAsset property. I guess this is not the case. How did you identify this? I just loaded via bundle.Load() and that worked much better.
– jasonFHH