- Home /
Mobile Optimization: Instantiate/Destroy vs. Enable/Disable (for non-frequent use)
Hello, I am working on a small 2D platformer-type game targeted for mobile devices (ios, android). I have some enemies/obstacles in the game like landmines or bombs that explode when the player collides with them and produce a particle effect. I was wondering what would be better optimization-wise for mobile devices, to destroy the objects and instantiate particles, or to disable (i.e. SetActive(false)) the object and enable the particles? The player is not likely to make objects explode very often, maybe every 10 seconds, so I am not worried about frequently calling instantiate/destroy (I am already using object pooling in those cases). Mobile devices are obviously limited in their hardware and memory, so I would like to use the best possible option for the smoothest gameplay.
Thank you in advance!
Answer by Lysander · Jan 04, 2018 at 01:29 AM
Object pooling is always better. Instantiation and destruction should, ideally, be limited to scene loads and not done in the middle of gameplay at all. This isn't just about mobile optimization, but a good rule of thumb in general- PCs and consoles have more memory, but they also tend to use far more just from higher resolution textures and more complex geometry. If you run into a memory problem, then just go over your pools and check to see where you can cut down on your initial pool sizes, see if there are any textures you can reduce the resolution for without a drastic visual change, and try to push as much data as possible into static data containers like ScriptableObjects instead of instance data objects (for instance, if you only have 3 distinct "versions" of bomb types, there's no reason every single bomb in the game should hold the data that distinguishes one from the other two). You can even keep separate pool counts from one scene to the next, depending on the maximum number of that kind of object the scene should be expected to have- just pre-instantiate all of the ones you might need at once.
If you have a kind of single event-occurrence in a scene that spawns a lot of objects once and only once, you may want to make a separate temporary pool for just that, pre-fill them to the extent you can on pre-load, and then Destroy (not Destroy Immediate) the pool when the event is complete to eventually get that memory returned.
A bomb that rarely explodes should still be pooled- everything that can be pooled, should be pooled. Even if you have two different bombs at separate parts of your stage, if they're not both visible simultaneously, then use the same bomb and just move it when you get in range. This is functionally identical to re-pooling objects when moving out of range, and pulling another out when moving into range, and can drastically cut down on memory usage. Just keep track of the status of each separately from the pooled object data (have a scene manager that can say "Bomb ID #16 has already blown up, no need to pull a pooled object when moving into range of it".
Make sense? Let me know if you have any more questions.
Thanks! That was just what I was looking for. I never thought about using objects in the pool for objects on and off screen, I have just been using them for projectiles. Good idea!
Your answer
Follow this Question
Related Questions
Mobile Game starts lagging after a while when playing on my phone!!!! Pls Help!!! 1 Answer
Mobile game, edges as colliders 0 Answers
Does Unity support different pixel densities? 1 Answer
Help with game, stopping player cheating (script help) 1 Answer
Is there any problem in using assets as prefabs instead of just gameobject? 0 Answers