Hi, I am currently working on my own GameObject pooling system. I want it to be as simple to use as possible even if that means sacrificing some of the speed.
I also want it to be able to manage all of the pools from one static class, even two same prefabs in different scenes.
There are two ways how to do that:
Store objects from different scenes separatly and than use the one that is in the same scene
transfer them between scenes using SceneManager.MoveGameObjectToScene()
I didnt knew which one is more efficient so I tried with Instantiating 1 000 000 empty GameObjects
which was basicly scenario 1 when pool is empty it took ~7.5s
Then I tried moving all of them to other scene and it took just ~700ms
I was suprised by that, because I was expecitng that MoveGameObjectToScene() will create new object in the other scene, copies values and then destroys the object in original scene, but it apparently does something lot smarter.
And I tested last thing, how fast is activating and deactivating Gameobjects in the same scene
Again I was suprised because using SetActive() on all 1 000 000 objects took twice as long as moving to a different scene ~1.5s
My question is, can I set up scene that is not updated and is not rendered?
It would be meaningfuly faster to move pooled objects to that scene instead of activating and deactivating them, at least according to my testing above.
Or maybe objects with more components and childs will be much slower to move than activate and deactivate them in their own scene?
Because I dont want to write new script for each object I want to pool
And also when using unitys methods for pools You still have to specify how the object is being “hidden” which means I still could use moving to other scene as an performance improvement
You could use the pool with some form of simple option of say a dictionary of pools, with a key of the prefab, have a component that stores the prefab or some unique ID or whatever floats your boat so you can pick the pool to return it to…
I am not trying to say that implementing object pool is difficult, but I think it is tedious. For each prefab You need to have reference to that pool, create few lines of code. Doing that for one or five prefabs is quick and easy, but doing that over and over in each project gets very tedious very quickly from my view point. Wouldn’t it be much quicker to use just two methods for example: prefab.Get(), Pool.Return(GameObject) and rest would be handled by the pooling system?
Maybe I didn’t understood some part of the Unitys inbuild pooling system? My main source of learning is from YouTube tutorials and documantation.
But I wasn’t able to find any great tutorial on Unity pooling system and when I checked docs for ObjectPool I got hit by largest wall of example code that I have ever seen (at least largest in Unity docs).
var inst = Lazarus.Instance.Summon(prefab);
Lazarus.Instance.RelenquishToPool(inst);
All pools will be managed automatically. You can configure how many elements are pre allocated at a time from Edit->Singletons. From there you can also opt to use Unity’s pooling system or my own as your backend allocator. Don’t use Unity’s. It’s bugged last time I checked. You could also implement your own. Note that there are some dependencies listed in the readme that you’ll also need.
well if you have multiple objects you need to pool dependant on the object type, yet you seem to hope its going to know how to do that for you, I have suggested a way to use the given object pool so yes, you arguably something like GiveMe(Bullet) or something, and it does, and return(this) when you want to get rid of it…
Now, it could be that rather than pools per prefab you decide on one mega pool and change the mesh/details accordingly … again, this is your right and choice and this is exactly why unity give you that flexibility. You write some code round it to make it work how you want… You cant expect unity to document every possibility - take a look in the asset store theres over 50 free pooling systems, and over 100 if you include paid…
Unity often gives tools not entire solutions because what works for you may not for me, but the tool to do either works for both
Big mistake!!
MissingReferenceException etc all over the place.
Unnecesary difficulties when domain reload is disabled, which you should disable because it makes entering playmode instantaneous!
And of course the classic project breaking non-architecture use of static classes for “managing” game object instances.
If you want 1 million game objects and instantiate them in a fraction of that time, use Entities (ECS).
Not if you properly engineer this into a reusable component script that simply gets added to each game object or prefab that should have pooling functionality. Whatever spawns these prefabs simply gets this component, then creates and destroys prefab instances through the general purpose pooling component.
The costs and issues associated with object pooling / pools:
In very rare extremely-high-count object circumstances I have seen small benefits from pooling.
In MOST circumstances, object pooling is a source of complexity, bugs and edge cases while not giving any measurable benefit.
Don’t be a cargo cult programmer and screw up your game with pooling that you don’t need and that won’t help you.
“Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose.”
Yes, You are right that adding code for marginal performance boost is bad idea, but I am preparing to create bullet hell game, which could later in game create hundreds or maybe even more projectijes a second. I think that is reasonable scenario to introduce pooling system.