示例#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;
 }