Can someone post a generic object pool script?

From what I can see everyone tells you that you need to use object pools. But finding a generic object pool script is way to hard for how common it’s said to be. So can someone post a C# and maybe a java script example that I can use, modify and point other programmers to?

It should have a example “Activate and set to position” method that is public.

The interface in the Inspector should look like this:

//

Object to pool [game object/transform/whateveryouusetoimporttheobject ]

Number to create [int]

//

That’s all I am asking for. Simple code that anyone can use and modify.

EDIT: My own implementation of ObjectPool It probably could benefit some modification, I don’t claim it to be perfect, it just works for me.

Here is a tutorial from Unity http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

The basic idea is to have a class that will store a list of game object, first you create:

List<GameObject>list;

public PoolSystem(int size, GameObject prefab){
     list = new List<GameObject>();
     for(int i = 0 ; i < size; i++){
         GameObject obj = (GameObject)Instantiate(prefab);
         list.Add(obj);
     }
}

This is the constructor of your pool. It takes a size and what object you want to store.

Then it has a method to get the first of the list

public GameObject GetObject(){
    if(list.Count > 0){ 
       GameObject obj = list[0];
       obj.RemoveAt(0);
       return obj;
    }
    return null;
}

this one is simplified as if you are sure of the list you can skip the check and if you don’t want to return null, you can create a new obect to be added to the list.

The point of taking the first of a list is that you do not have to search for a free one while using the array.

The other part is to reassign the object to the pool:

public void DestroyObjectPool(GameObject obj){
     list.Add(obj);
     obj.SetActive(false);
}

Finally you may want to clear the pool:

public void ClearPool(){
    for(int i = list.Count - 1; i > 0; i--){
        GameObject obj = list.RemoveAt(i);
        Destroy(obj);
    }
    list = null;
}

Though this last part is only useful if you need to clear a pool but if you do all properly you should not need that since the point of a pool is that you do not have to create and destroy, just create. The GC will take care of removing it all

I use List over Stack or Queue because it goes faster according to a test I did a while ago. You could also look here :

http://msdn.microsoft.com/en-us/library/ff458671%28v=vs.110%29.aspx

but I have never used nor seen ConcurrentBag apart from that example.

The Unity tutorial is great.

I also have a blog post here that works on a slightly different principle

So I checked out the tutorial from unity [Object Pooling][1]. And I recommend that you watch it if you need to understand more. Mike Geig does a great job of explaining how it works.

OK now for the script. This is in C# and if someone can get a copy in java it would be a good idea.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObjectPoolScript : MonoBehaviour
{
	public GameObject pooledObject;
	public int pooledAmount = 20;
	public bool willGrow = true;
	
	public List<GameObject> pooledObjects;
	
	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 *== null)*
  •  	{*
    
  •  		GameObject obj = (GameObject)Instantiate(pooledObject);*
    
  •  		obj.SetActive(false);*
    

_ pooledObjects = obj;_
_ return pooledObjects*;
}
if(!pooledObjects.activeInHierarchy)
{
return pooledObjects;
}
}*_

* if (willGrow)*
* {*
* GameObject obj = (GameObject)Instantiate(pooledObject);*
* pooledObjects.Add(obj);*
* return obj;*
* }*

* return null;*
* }*

}
So to use this script use some code like this
ObjectPoolScript objectPoolScript;

And in the Start method set it to
GetComponent ();
and then to fire a bullet (which is what I needed it for) do something like
void Fire()
* {*
* GameObject obj = NewObjectPoolerScript.current.GetPooledObject();*

* if(obj == null) return;*

_ bullets*.transform.position = transform.position;
bullets.transform.rotation = transform.rotation;
bullets.SetActive(true);*_

* }*
Anything else you want to do is up to you. Like I said this is supposed to be a script that you can modify and add to. Oh and I did test this and it works well. And if you have a lot of things that need use the same GameObjects you can set it up so that everything that is in the scene can access the script you should watch the video at the link above.(it’s in the last half that he builds the generic script and sets it up) And this really is not my script I just did a little edit so that I could attach it to my weapon object. Copy and Paste the code modify it if you need to and make a great game.
_*[1]: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling*_

The other answers here pretty much cover it (definitely worth going through!), but there is this implementation here. I’ve not used it myself, but it seems legit enough.

It’s not too tough to implement yourself though, and the other answers tell you pretty much everything you’d need to know for that =).