Search Unity

Question Better pooling system?

Discussion in 'Scripting' started by PHOENIX33201, Jan 6, 2024.

  1. PHOENIX33201

    PHOENIX33201

    Joined:
    Apr 16, 2022
    Posts:
    12
    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:
    1. Store objects from different scenes separatly and than use the one that is in the same scene
    2. 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?
     
    swimswim likes this.
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    474
  3. PHOENIX33201

    PHOENIX33201

    Joined:
    Apr 16, 2022
    Posts:
    12
    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
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,890
    No.

    What problem are you trying to solve here? You want "simple as possible" but then list a huge retinue of requirements. You can only have one.

    Unity already culls rendering for objects not viewed by any camera.
     
    Sluggy likes this.
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777

    Well, in some ways you dont.

    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...
     
  6. PHOENIX33201

    PHOENIX33201

    Joined:
    Apr 16, 2022
    Posts:
    12
    I want it simple to use and versitile, it might be difficult to programme at first.
     
  7. PHOENIX33201

    PHOENIX33201

    Joined:
    Apr 16, 2022
    Posts:
    12
    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).
     
  8. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    987
    https://github.com/Slugronaut/Lazarus

    Code (CSharp):
    1. var inst = Lazarus.Instance.Summon(prefab);
    2. 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.
     
    Last edited: Jan 6, 2024
    SisusCo likes this.
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    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
     
  10. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,909
    Don't!

    This one is awesome and free: https://assetstore.unity.com/packages/tools/utilities/master-object-pooler-2-146525

    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.
     
    Nad_B likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,708
    The costs and issues associated with object pooling / pools:

    https://forum.unity.com/threads/object-pooling.1329729/#post-8405055

    https://forum.unity.com/threads/object-pooling-in-a-tower-defense-game.1076897/#post-6945089

    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.

    https://en.wikipedia.org/wiki/Cargo_cult_programming

    "Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose."
     
  12. PHOENIX33201

    PHOENIX33201

    Joined:
    Apr 16, 2022
    Posts:
    12
    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.
     
  13. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    perfect for dots then