Пример #1
0
        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);
        }
Пример #2
0
 public static void Despawn(PoolComponent poolObject)
 {
     foreach (var component in poolObject.GetComponents <SpecialComponent>().ToList())
     {
         Object.Destroy(component);
     }
     poolObject.Pool.Despawn(poolObject);
 }
Пример #3
0
        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);
        }
Пример #4
0
 public static PoolComponent Spawn(PoolComponent original, Vector3 position, Quaternion rotation, Transform parent = null)
 {
     return(GetOrCreatePool(original).Spawn(position, rotation, parent));
 }
Пример #5
0
 public void Despawn(PoolComponent poolObject)
 {
     poolObject.gameObject.SetActive(false);
     _inactiveObjects.Push(poolObject);
 }
Пример #6
0
 public Pool(PoolComponent prefab)
 {
     _prefabComponent = prefab;
 }