/// <summary> /// 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. /// </summary> /// <param name="objectIdentifier">The identifier you want to use to identify the kind of game object you want to retrieve.</param> /// <param name="position">The position that the game object should have before it is activated.</param> /// <param name="rotation">The rotation that the game object should have before it is activated.</param> public GameObject GetGameObject(string objectIdentifier, Vector3 position, Quaternion rotation) { GameObject obj = null; GameObjectCreator creator = null; if (_creators.ContainsKey(objectIdentifier)) { creator = _creators[objectIdentifier]; } else { Debug.Log("Unable to create a GameObject for object ID '" + objectIdentifier + "' because no IGameObjectCreator implementation can be found for it."); return(null); } EnsureListForObjectID(objectIdentifier); Queue <GameObject> objects = _pool[objectIdentifier]; if (objects.Count > 0) { obj = objects.Dequeue(); } else { obj = creator.Instantiate(); } if (obj != null) { creator.PrepareForUse(obj); obj.SetActive(true); } return(obj); }
/// <summary> /// 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. /// </summary> /// <param name="creator">The implementation of IGameObjectCreator to use for GameObjects associated with the objectIdentifier.</param> /// <param name="objectIdentifier">The identifier you want to use to identify the kind of game objects you want to create.</param> public void AddCreator(GameObjectCreator creator, string objectIdentifier) { _creators.Add(objectIdentifier, creator); }