GameObjectPool Class

Definition

Used to recycle Unity GameObjects. When ever you create GameObjects during runtime some overhead is incurred. Additionally memory can become highly fragment as well as possibly causing the garbage collector to perform a collection (which is also a performance hit). This is especially prevalent when you are spawning and destroying GameObjects of the same type very quickly in large quantities (such as bullets). The GameObject pool allows you to recycle objects so they can be reused upon request.

C++
public ref class GameObjectPool
Inheritance
GameObjectPool

Examples

Setup code for using the generic prefab instance creator:

GameObjectPool pool = new GameObjectPool();
GenericPrefabInstanceCreator creator = new GenericPrefabInstanceCreator();
creator.Prefab = MyProjectilePrefab;
pool.AddCreator(creator, "projectile1");

Requesting a game object from the pool:

var myProjectileObj = pool.GetGameObject("projectile1");

Recycling the game object:

pool.Recycle(myProjectileObj, "projectile1");

Remarks

Note that the GameObjectPool is not thread safe. It should only be used in Unity's main thread.

Constructors

GameObjectPool()

Initializes a new instance of the GameObjectPool class.

Methods

AddCreator(GameObjectCreator, String)

GameObjects are created by an implementation of IGameObjectCreator in this GameObjectPool. This method adds your implementation of the IGameObjectCreator to use for objects that share a specific object identifier.

Count(String)

Gets the number of game objects in the pool for a specific identifier.

EmptyPool()

Removes and destroys all game objects in the pool.

EmptyPool(String)

Removes and destroys all game objects in the pool associated with the specified objectIdentifier.

GetGameObject(String, Vector3, Quaternion)

Gets a game object for a specific object identifier from the GameObjectPool. If the kind of game object being requested is not in the pool, then it will get created by a IGameObjectCreator that was added to the pool for handling objects associated with the objectIdentifier.

GetGameObject(String)

Same as calling GetGameObject(objectIdentifier, Vector3.zero, Quaternion.identity)

Recycle(GameObject, String)

Adds a game object under a specific object identifier to the GameObjectPool.

Applies to

Produkt Wersje
MRTK2 Unity 2018 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.6.0, 2.7.0
MRTK2 Unity 2019 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.6.0, 2.7.0, 2.8.0
MRTK2 Unity 2020 2.5.2, 2.5.3, 2.5.4, 2.6.0, 2.7.0, 2.8.0