Class GameObjectPool
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.
Inherited Members
Namespace: Microsoft.MixedReality.Toolkit.Utilities.GameObjectManagement
Assembly: cs.temp.dll.dll
Syntax
public class GameObjectPool
Remarks
Note that the GameObjectPool is not thread safe. It should only be used in Unity's main thread.
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");
Constructors
GameObjectPool()
Initializes a new instance of the GameObjectPool class.
Declaration
public GameObjectPool()
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.
Declaration
public void AddCreator(GameObjectCreator creator, string objectIdentifier)
Parameters
Type | Name | Description |
---|---|---|
GameObjectCreator | creator | The implementation of IGameObjectCreator to use for GameObjects associated with the objectIdentifier. |
String | objectIdentifier | The identifier you want to use to identify the kind of game objects you want to create. |
Count(String)
Gets the number of game objects in the pool for a specific identifier.
Declaration
public int Count(string objectIdentifier)
Parameters
Type | Name | Description |
---|---|---|
String | objectIdentifier |
Returns
Type | Description |
---|---|
Int32 |
EmptyPool()
Removes and destroys all game objects in the pool.
Declaration
public void EmptyPool()
EmptyPool(String)
Removes and destroys all game objects in the pool associated with the specified objectIdentifier.
Declaration
public void EmptyPool(string objectIdentifier)
Parameters
Type | Name | Description |
---|---|---|
String | objectIdentifier | The identifier you want to use to identify the kind of game objects to remove from the pool. |
GetGameObject(String)
Same as calling GetGameObject(objectIdentifier, Vector3.zero, Quaternion.identity)
Declaration
public GameObject GetGameObject(string objectIdentifier)
Parameters
Type | Name | Description |
---|---|---|
String | objectIdentifier | The identifier you want to use to identify the kind of game object you want to retrieve. |
Returns
Type | Description |
---|---|
GameObject |
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.
Declaration
public GameObject GetGameObject(string objectIdentifier, Vector3 position, Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
String | objectIdentifier | The identifier you want to use to identify the kind of game object you want to retrieve. |
Vector3 | position | The position that the game object should have before it is activated. |
Quaternion | rotation | The rotation that the game object should have before it is activated. |
Returns
Type | Description |
---|---|
GameObject |
Recycle(GameObject, String)
Adds a game object under a specific object identifier to the GameObjectPool.
Declaration
public void Recycle(GameObject gameObject, string objectIdentifier)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | The GameObject to recycle. |
String | objectIdentifier | The identifier you want to use to identify the kind of game object you are recycling. |