GameObjectPool Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
public ref class GameObjectPool
public class GameObjectPool
type GameObjectPool = class
Public Class GameObjectPool
- Inheritance
-
GameObjectPool
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");
Note that the GameObjectPool is not thread safe. It should only be used in Unity's main thread.
Game |
Initializes a new instance of the GameObjectPool class. |
Add |
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. |
Empty |
Removes and destroys all game objects in the pool. |
Empty |
Removes and destroys all game objects in the pool associated with the specified objectIdentifier. |
Get |
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. |
Get |
Same as calling GetGameObject(objectIdentifier, Vector3.zero, Quaternion.identity) |
Recycle(Game |
Adds a game object under a specific object identifier to the GameObjectPool. |
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 |