Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Andrei-Maroiu · Mar 29, 2017 at 02:50 PM · 2d gameoptimizationobject pool

When should I use object pool?

I am doing a 2D game that generate at every second a game object. And the game object is different every time, for that I am using like 12 prefabs, but 9 of then are pretty the same, the only difference is the sprite. So there are 3 very different prefabs with different children, diffrent scripts , and different polygon collider and size. So to do a object pool for them I need to change the size , the sprite , the child and the polygon collider. for that and need every time to destroy some components and adding them.

So should I use the object pooling or just instantiate and destroy them?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hanoble · Mar 29, 2017 at 06:36 PM 0
Share

You mention this:

"So to do a object pool for them I need to change the size , the sprite , the child and the polygon collider. for that and need every time to destroy some components and adding them."

However, depending on your game needs you may not want to do that at all. If there is not a ton of randomization, you could simply pool a dozen of those objects, knowing you will only ever have x amount at a time, and reuse those objects over and over. Pooling does not necessarily mean you need to have a handful of objects that are constantly manipulated, you could ins$$anonymous$$d have more objects that remain the same. It really comes down to your game and where you have the resources to spend.

With that being said, if you are looking to hit a high FPS on lower end mobile, object pooling is almost a requirement at this point. Lucky for you, there are some good resources out there for learning about object pooling and a slew of pooling systems on the asset store you can look in at well, even some free ones!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Pan_Migo · Mar 30, 2017 at 02:03 PM

Well because you want to generate every second a game object, object pooling is far better in terms of performance (most specifically CPU and memory), than instantiating and destroying. Also, I assume that because you make a 2d game you may want to target for web or mobile, where the community widely suggests using object pooling.

So even in case, where pooling objects concerning performance may not have too much difference in your game I highly suggest you learn how to do it because some day you might want to use it. I recently created a generic object pooling class based on the unity tutorials, which you can use for any object you want :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjectPooler : MonoBehaviour {

     public static ObjectPooler current;
     public GameObject pooledObject;
     public int pooledAmount = 11;
 
     //list used for object pooling
     List<GameObject> pooledObjects;
 
         //reference this game object
     void Awake() {
         current = this;
     }
 
     // Use this for initialization
     void Start () {
         
         pooledObjects = new List<GameObject>();
         for(int i =0; i < pooledAmount; i++){
             GameObject obj = (GameObject) Instantiate(pooledObject);
             obj.SetActive(false);
             pooledObjects.Add(obj);
         }
     
     }
 
     
     public GameObject GetPooledObject(){
         for(int i=0; i < pooledObjects.Count ; i++)
         {
             if(!pooledObjects[i].activeInHierarchy)
             {
                 return pooledObjects[i];
             }
         }
         
         return null;
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Rhombuster · Mar 29, 2017 at 06:00 PM

This is more of a judgement call. It depends on the complexity of your code, the frequency you're creating objects, the platform you're targeting, so I can't say you definitively should or shouldn't based on the information you've given. However, there is a resource I use that is free and has some guidelines. I'd suggest reading it: http://gameprogrammingpatterns.com/object-pool.html

Here is a snippet about when to use object pools:

 This pattern is used widely in games for obvious things like game entities and visual effects, but it is also used for less visible data structures such as currently playing sounds. Use Object Pool when:
 
 You need to frequently create and destroy objects.
 
 Objects are similar in size.
 
 Allocating objects on the heap is slow or could lead to memory fragmentation.
 
 Each object encapsulates a resource such as a database or network connection that is expensive to acquire and could be reused.
 




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimization Pun2 send hundreds of rpcs 0 Answers

Q: How can I resolve my error and how to optimize this script so the game runs smooth ? 2 Answers

Movement on prefab objects 0 Answers

Unity profiler Gfx.WaitForPresentOnGfxThread 2 Answers

Shooting using an object pool 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges