Lag when Instantiating a loaded AssetBundle

Hi!

I have a 3D model I’ve made into an AssetBundle. I load the AssetBundle using yield etc. on my iPhone and it works great, no lag. It does it in the background.

But when I Instantiate the actual model after loading it, it lags for 1+ sec when adding it to the scene. I call the Instantiate in a StartCoroutine() function, so it should be doing it in its own thread.

Is there any way to make it instantiate it without any lag?

The 3D model has 150 animations, but I can’t have all of those loaded at once because of memory issues, so what I’ve done is packaged each FBX animation into its own AssetBundle, so I’m trying to load an animation when needed at runtime and play it, without any noticeable lag.

Thanks!

Using the bundle.mainAsset the first time is like doing a bundle.Load(…) and it is not async. In my case we use WWW.LoadFromCacheOrDownload and then the first call to bundle.mainAsset takes 0.076 sec and allocates ~8MB of memory. This is the whole bundle, unpacked. What I then have is the prefab as it was packed in my bundle. I can now Instantiate it to get it into the scene. If you use bundle.LoadAsync(…) you can remove the fps drop of the loading (Just don’t use the mainasset.name to find the name! ).

When it come to the Instantiate, it might be more complicated depending on how complicated your prefab is, I assume. How much code it runs in awake etc. It might also help to think about what other things happens the same frame. Maybe add a yield return null; before and after doing the Instantiate. If you do a lot of Instantiate then try spreading them over several frames as well.

A short lag will always be there, thats caused by the texture upload when the object first shows in front of the camera.
Using smaller textures, compressed textures and alike helps seriously on that end.

also, you can load data asyncronously from asset bundles if you use the corresponding load function instead of Instantiate with the mainAsset