(Better) Object Pooling - I Didn't Like My Old Solution

Object pooling is an easy way to get a performance boost in Unity or any other game engine. Reusing objects is faster than constantly creating and destroying objects. This method requires no object pool manager, allows easy initialization, and most important allows objects to automatically return themselves to the pool. Using C#, interfaces, actions, and generics this solution is highly extendable and easy to use.
Object Pool Code On Github: github.com/onewheelstudio/Adv...
Generics Video: • Generics in C# and Uni...
Actions/Events Video: • Delegates, Events, Act...
Want to support the Channel?
*******************************
Buy me a coffee: www.buymeacoffee.com/onewheel...
Shop the Unity Asset Store (affiliate): assetstore.unity.com/top-asse...
Become A Channel Member: / @onewheelstudio
Or a Patron: / onewheelstudio
My video and streaming kit: kit.co/onewheelstudio/tutoria...
Buy a Game Design Book: kit.co/onewheelstudio/game-de...
Video Gear List (Amazon Affiliate)
***********************************
Sony A6100 Camera: amzn.to/32logXz
Sigma 16mm 1.4 Camera Lens: amzn.to/3KA4Wr4
GoXLR (audio interface): amzn.to/35dmr03
Shure SM7B Mic: amzn.to/3fNbV1E
Blue Mic Arm: amzn.to/3tHgwdQ
Neewer Light: amzn.to/3rD8eRv
Light Softbox: amzn.to/3GUaHgQ
Neewer RGB Light: amzn.to/3rCTQJ9
Other Links
************
Discord: discord.onewheelstudio.com
Twitter: / onewheelstudio
Devlog: onewheelstudio.com/
Twitch: / onewheelstudio
Timestamps
*************
0:00 Why?
0:48 What is Object Pooling?
1:18 Implementation
2:16 Interfaces
2:29 IPool
2:51 IPoolable
3:21 Object Pool Class
4:29 Constructors
4:51 Pull Function
5:47 Push Function
6:31 Pool Object
7:35 Pull Function Overloads
8:07 Using Pull & Push Actions
8:30 THE END
#GameDevelopment #Unity3D #indiedeveloper

Пікірлер: 53

  • @OneWheelStudio
    @OneWheelStudio2 жыл бұрын

    Object Pool Blog Post: onewheelstudio.com/blog/2022/1/17/object-pooling-20 Object Pool Code On Github: github.com/onewheelstudio/Adventures-in-C-Sharp/tree/main/Object%20Pool%202

  • @ahmay
    @ahmay2 жыл бұрын

    I have been developing games with unity for over 4 years and never seen this channel before. Precisely edited and explained semi-advanced topics as quick as possible. I appreciate the effort put into these videos.

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Your description is exactly the goal. Hope the videos are useful!

  • @jakubstacho9199
    @jakubstacho91992 жыл бұрын

    Great channel, so underrated. I've been creating games in Unity for 4 years, but there is still so much to learn, thank you for the videos!!!

  • @jeffreyhersh908
    @jeffreyhersh9082 жыл бұрын

    What an interesting implementation of object pooling. I am definitely going to adapt this for my projects.

  • @lee1davis1
    @lee1davis12 жыл бұрын

    Added to my Unity must watch Playlist. Thanks for sharing.

  • @mrvastayan
    @mrvastayan2 жыл бұрын

    Glad to see you're still making videos, I recently started teaching myself actual C# got some decent progress going on a project now :)

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Yep still making them. They come in bursts. In a bit of a dry spell at the moment. C# eh? Nice. How's that going for you? Happy to help if I can.

  • @mrvastayan

    @mrvastayan

    2 жыл бұрын

    @@OneWheelStudio cheers I appreciate that! Yeah its going really well so far. Bolt gave me a really good foundational understanding to work from :)

  • @goehlergamedev
    @goehlergamedev Жыл бұрын

    Stellar tutorial! Really enjoyed it. One small optimization tip: Using the transform.SetPositionAndRotation function is a bit more performant than setting .position and then .rotation

  • @ewwitsantonio
    @ewwitsantonio2 жыл бұрын

    Great explanation. I really like this structure a lot. I'll have to test it out. Thanks for sharing this video with us :)

  • @ZenryokuFGC
    @ZenryokuFGC Жыл бұрын

    Concise and clear explanation in the video with no fluff, excellent code with example I'm using in my project right now - one of the easiest likes + subscribes I've ever done. Thank you

  • @MarekNijaki
    @MarekNijaki2 жыл бұрын

    Your channel is a hidden gem! Hope you get more subscribers fast 😃

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Keeping it hidden - part of my master plan :P I hope there's some useful stuff here for you! Cheers!

  • @TheKr0ckeR
    @TheKr0ckeR2 жыл бұрын

    I literally yesterday understood Old Object Pool totally and used it a lot! :D Now here is a new one to understand

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Just trying to keep you on your toes! 😉

  • @somehappyballs6637
    @somehappyballs66372 жыл бұрын

    I like the "public T pull (vector3 position, quaternion values)" function ! Great idea

  • @Inkognitohaloramics
    @Inkognitohaloramics6 ай бұрын

    Thanks for the video! This is super helpful

  • @cvnvr
    @cvnvr2 жыл бұрын

    another great video. thanks for putting this together! one small thing... i noticed at 4:51 your second constructor example seems to be missing the same 'Spawn' call as the first? the numToSpawn var isn't being used at all in this one. considering both are seemingly doing the same thing, the second just includes the additional Actions being passed in, it could call : this(pooledObject, numberToSpawn) to avoid unwanted inconsistencies in behaviour/duplicating code. unless i misunderstood the purpose of the second constructor, then apologies and please ignore!

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    That was just a mistake - I wondered how long it would take for someone to notice ;) I didn't catch it until after the video was released. I fixed it in the GitHub repo (link in the description).

  • @devforfun5618
    @devforfun56182 жыл бұрын

    if i have multiple game objects using the same script in my case Projectile will this treat any projectile interchangeably regardless of gameobject ? do i need to create a subclass for each projectile in this case ?

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    If the projectile class is being used on multiple prefabs and the projectile class is the owner of the object pool then yes this object pool solution will treat all projectiles the same and a solution would be to subclass each type of projectile. What I'd recommend instead is that whatever object is shooting the projectile is the object that owns the object pool. Assuming each object only shoots one type of projectile it would solve the problem. If that doesn't work you can look at the code for my spawner (from the demo at the beginning of the video). In there I have 3 objects pools, one for each of the prefab shapes. Github Link: github.com/onewheelstudio/Adventures-in-C-Sharp/tree/main/Object%20Pool%202 To go a step further, in my personal project all of the projectile information (damage, speed, prefab, explosion prefab) is on a scriptable object. This way I just assign the SO to the object shooting AND the SO itself (one instance for each type of projectile) owns the object pool.

  • @devforfun5618

    @devforfun5618

    2 жыл бұрын

    @@OneWheelStudio im using scriptable objects too, except for the visuals of the projectiles, but i guess making the owner of the object control the pool works better this solves another problem, the local player will be able to instantiate their projectiles instead of sending a request to the host to do do that, since the pool is per player, not per type of projectile and knowing which weapons a player has, and the fire rate, i know how exactly how many projectiles i need to spawn at the start thanks for the help

  • @user-gj7eb6hb1y
    @user-gj7eb6hb1y8 ай бұрын

    can you have more example for objects that can be implement the IPoolable interface, and what the effective of this interface when we use it

  • @OneWheelStudio

    @OneWheelStudio

    8 ай бұрын

    Come join the OWS discord. Happy to chat. I just created a class that implemented IPoolable yesterday! The main reason I did that is it stores the component rather than the game object which means I don't have to do GetComponent each time I pull an object out of the pool.

  • @TheKr0ckeR
    @TheKr0ckeR2 жыл бұрын

    It's really cool! I have a one question. In this example, we spawn over time. So we dont have a pool at start. But how would be achieve something like that we create 50 objects, and pull from that pool? Should i just basically spawn in a loop, and push them to the pool, then i pull what i want?

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Should be even easier if you're using the full code! The constructors have an optional parameter for a start count. You can simply pre-spawn objects when you create the pool. That said, the spawning is not done over multiple frames so depending on the object creating 50 in one frame may cause a lag spike. If the built-in system does create a lag spike, then I'd use a coroutine that over a handful of frames pulls whatever number of objects you need and them turns them all off to return them to the pool.

  • @GooeyDev
    @GooeyDev Жыл бұрын

    Hey, I am currently trying toadd pool to my game for an endless runner 3d and my character does not move in the z axis. My issue is that I can't spawn the roads as new ones gets deleted as I am unable to get the last activated tile from the pool and spawn it with an offset. Could someone help me?

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    I’m not sure if exactly what the problem is. Are you actually deleting the roads? For the object to go back to the pool it should just be disabled not turned off. If that’s not the issue it may be a bit more complicated - feel free to jump over to the OWS discord it might be easier to help there.

  • @rashidfarhan6223
    @rashidfarhan6223 Жыл бұрын

    hi! I'm kinda confused how should I approach the problem where I have multiple prefabs with a script(say : projectile) that is inherited from PoolObject.

  • @rashidfarhan6223

    @rashidfarhan6223

    Жыл бұрын

    public ObjectPool bulletPool; my pool was static. that's why. it works now as intended!

  • @TheKr0ckeR
    @TheKr0ckeR Жыл бұрын

    I am using this for a long time. I have generally problems at similar objects. What approach should we look if we have "Prefab variants" for example different enemy types, Should i create pool for every enemy type or what would be your apporach, like creating a dictionary or smth? Can you give a little information about that? Every time a new enemy added, I need to create a new pool. Instead I will be able to add it enemy pool and check it with LINQ or smth.

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    Hmm. That's an interesting question. My first thought would be to create a new pool for each variant and just use the system that way. But your suggestion of using a dictionary could also work in a couple different ways. You could make a dictionary that has a key of some type of enum (for example) with an item for each variant and then the value of the dictionary could be an object pool. Or you could flip that and create a new kind of object pool by implementing IObjectPool and put the dictionary inside the pool - this is probably more work on your part. I'd probably go for the first option if your enemies are the only use case if you have more use cases it might be useful to take the time to setup the new type of pool. Not sure if that helps.

  • @themrfj
    @themrfj Жыл бұрын

    How is your ObjectPool different from the built in ObjectPool?

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    It’s been a while since I made it and don’t remember all the ins and outs of the built in system - it’s likely been updated too? I remember reading complaints of memory leaks too. 🤷‍♂️ At the end of the day the biggest different is with my own system I can adapt and adjust to code. I can also pool any type of object (can’t remember if the built in system will do that).

  • @shivamanand25
    @shivamanand25 Жыл бұрын

    For anyone getting problem ,If its giving Null reference error on line 43 attach "PoolObject" Script to Poolobject or Prefabs that u wanna spawn.

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    On mobile atm, is it possible the objects are getting destroyed instead of disabled?

  • @shivamanand25

    @shivamanand25

    Жыл бұрын

    @@OneWheelStudio no, they're all getting disabled, I checked

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    Hmm. Not sure I’d try a break point and check what is null. I use this system in my own project so it should work.

  • @shivamanand25

    @shivamanand25

    Жыл бұрын

    @@OneWheelStudio can I use this system for multiple gameobjects ? I mean there are multiple things to instantiate like coins, particles, bullets etc.

  • @OneWheelStudio

    @OneWheelStudio

    Жыл бұрын

    @@shivamanand25 Sure. Create a new pool for each type of object and then pull from which ever pool you need. If you want to combine multiple types into a single pool. That could be done, but I probably wouldn't go that direction.

  • @samuelsatiro2576
    @samuelsatiro25762 жыл бұрын

    I tried to use your pooling but I couldn't because it's giving null reference error -_- (Unity 2020.3)

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    Hmm. Strange. Can you be more specific as to what line is null or the use case?

  • @samuelsatiro2576

    @samuelsatiro2576

    2 жыл бұрын

    @@OneWheelStudio The error is in the ObjectPool script (Line 43). For some reason the T is returning null.

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    @@samuelsatiro2576 The only reason I can think of for why that line would be null is if the object was destroyed. If I had to guess an object was put back in the pool and then destroyed? If you can't figure out the cause, you could put in a null check just above line 43 - check if T is null. If it is then "return Pull()" This would effectively discard the object and pop the next one out of the stack.

  • @samuelsatiro2576

    @samuelsatiro2576

    2 жыл бұрын

    @@OneWheelStudio I made the modification you said, my Unity crashed lol

  • @OneWheelStudio

    @OneWheelStudio

    2 жыл бұрын

    That is very odd. If you jump over to the OWS Discord. It would be helpful to see the code. For me the question is why is the object null? What is destroying the object?

  • @mohokhachai
    @mohokhachai10 ай бұрын

    Game object is s data type

  • @alexproskunov4001
    @alexproskunov40012 жыл бұрын

    1. no example of using. 2. over-engineering code. 3. Stack.

  • @everlastingmedia

    @everlastingmedia

    2 жыл бұрын

    1. no example of an example you'd like used 2. failure to explain how code was over-engineered 3. "Stack" what?