Loading prefabs into memory at runtime

Hi,
I am trying to make a game like Swamp Attack . As you progress through the game, more enemies are unlocked.

The playing scene remains the same, so instead of creating separate scene for each stage, i will create a single scene that loads the type, number and order of enemies through an xml file.

Right now, all the enemies’ prefabs are being used by placing them in public objects of a script. This would mean they are all loaded (their scripts and textures/animations etc), so they are placed in RAM even if they are not on screen.

I want to know how i should build my script such that only those monsters are loaded into RAM which are mentioned in the xml file for that stage.

The best way to do this is with an ObjectPool. When you load a new stage, compile a list of all the resources you need to use with your xml file and load the prefabs via ObjectPool.LoadAll(resourcePaths) which should wrap Resources.Load(resourcePath). Add two more functions to the ObjectPool. One to get an instance of the prefab and one to return the instance to the pool for recycling (ObjectPool.GetInstance(resourcePath), ObjectPool.RecycleInstance(instance)). Then you just need a way to reset your instances to the default prefab values when they are recycled. When you unload the stage destroy all the object instances via MonoBehaviour.DestroyImmediate(instance) and use something like ObjectPool.UnloadAllAssets() which should wrap Resources.UnloadAsset(prefab)

Thank you for your answer, but i had already looked up on how to use the resources folder.
But from this post i was scared off resources as it mentions that even if resources are not used it takes up RAM.
http://answers.unity3d.com/questions/352882/memory-overhead-associated-with-assets-in-resource.html

From the post : (It is quite an old post though 2012, maybe things have changed now)

Why you want to use Resources folder… Instead take all the enemies reference inside an public array. And then use some logic according to current level and show only those enemies from array

This is what i am doing right now. But if the prefab is referenced doesnt it mean that it and its children are in RAM?
Each enemy has animated sprites. So these sprites are also in RAM. Or am I mistaken?

You don’t need to worry about the mainData file. If you can produce an abnormally large one it is most likely a bug and should be reported. The Resource functions are there to provide a way to load and unload data safely. If you add a reference to a prefab in your scene, the prefab will be loaded with the scene.

You also have the option to use asset bundles.
The resources folder basically turns all assets within into a giant assetbundle.
You would build said bundles in the editor and put it inside the StreamingAssets folder for easy runtime access via the Application.streamingAssets property.
Then in runtime you would load the bundle with the required assets and for example instantiate the enemy prefab contained within it. You must also unload it whenever you no longer need the assets inside.
I must ask though if you’ve already reached the RAM capacity for not having everything loaded from start, because it’s simply the easiest way to go about things.

1 Like

Thank you @jimroberts and @ThermalFusion

@jimroberts So the prefabs in the resources folder that i dont use, are they in RAM or they are loaded into memory only when i use Resources.Load?

@ThermalFusion I have only made 10 different enemies right now, so i am sure the RAM capacity hasnt been reached. But my plan is to make at least 50. I want to know the best way from the beginning, i dont want to change my code later. I will look into StreamingAssets. Though Resources folder seems a bit easier right now.

The prefab will only be loaded into memory when you call Resources.Load and the memory will be released when you call Resources.Unload on the prefab. The Resources class is just a wrapper for the internal C++ resource loading functions. You will be fine just setting up an ObjectPool that can recycle instances of your prefabs like I described in my original reply.

1 Like

Thank you for this. I am already using object pool for all enemies, projectiles etc. But i had referenced each prefab in the script. So even if that enemy wasnt needed in that scene it was loaded. Now using the Resources folder like you guided i will not waste memory.