public PoolComponent Spawn(Vector3 position, Quaternion rotation, Transform parent = null) { PoolComponent poolObject = null; while (_inactiveObjects.Count > 0 && (poolObject = _inactiveObjects.Pop()) == null) { // do nothing, just pop those items, until we find valid item or empty the stack. // invalid objects could be destroyed by Object.Destroy method somewhere else. } if (ReferenceEquals(poolObject, null)) { // we didn't find the object to reuse poolObject = Object.Instantiate(_prefabComponent.gameObject, position, rotation).GetComponent <PoolComponent>(); poolObject.gameObject.name = $"{_prefabComponent.name} ({_index++})"; poolObject.Pool = this; } var gameObject = poolObject.gameObject; gameObject.transform.position = position; gameObject.transform.rotation = rotation; if (!ReferenceEquals(parent, null)) { gameObject.transform.parent = parent; } gameObject.SetActive(true); return(poolObject); }
public static void Despawn(PoolComponent poolObject) { foreach (var component in poolObject.GetComponents <SpecialComponent>().ToList()) { Object.Destroy(component); } poolObject.Pool.Despawn(poolObject); }
private static Pool GetOrCreatePool(PoolComponent poolPrefab) { if (Pools.TryGetValue(poolPrefab, out var result)) { return(result); } // new pool result = new Pool(poolPrefab); Pools.Add(poolPrefab, result); return(result); }
public static PoolComponent Spawn(PoolComponent original, Vector3 position, Quaternion rotation, Transform parent = null) { return(GetOrCreatePool(original).Spawn(position, rotation, parent)); }
public void Despawn(PoolComponent poolObject) { poolObject.gameObject.SetActive(false); _inactiveObjects.Push(poolObject); }
public Pool(PoolComponent prefab) { _prefabComponent = prefab; }