Ejemplo n.º 1
0
    public PoolObject Create(PoolSystem poolSystem, Transform parent)
    {
        PoolObject nonTakenObject = null;

        if (poolSystem.poolObjects != null && poolSystem.poolObjects.Count > 0)
        {
            nonTakenObject = poolSystem.poolObjects.FirstOrDefault(t => t.inUse == false);
            if (nonTakenObject == null)
            {
                nonTakenObject = GameObject.Instantiate(poolSystem.poolObject);
                poolSystem.poolObjects.Add(nonTakenObject);
            }
        }
        nonTakenObject.Create(parent);
        return(nonTakenObject);
    }
Ejemplo n.º 2
0
        /// <summary>Spawn object using pools. If same object already was pooled and inactive now, it will be re-activated, not instanced again.</summary>
        public static GameObject Spawn(GameObject prefab)
        {
            var prefabHash = prefab.GetHashCode();

            PoolObject poolObject = null;

            if (pools.ContainsKey(prefabHash))
            {
                var pooledCollection = pools[prefabHash];

                for (var i = 0; i < pooledCollection.Count; i++)
                {
                    var comparePoolObject = pooledCollection[i];

                    if (comparePoolObject.IsPooled)
                    {
                        poolObject = comparePoolObject;
                        break;
                    }
                }

                if (poolObject == null)
                {
                    poolObject = PoolObject.Create(prefab);
                    pooledCollection.Add(poolObject);
                }
            }
            else
            {
                poolObject = PoolObject.Create(prefab);
                pools.Add(prefabHash, new List <PoolObject> {
                    poolObject
                });
            }

            poolObject.Unpool();

            return(poolObject.Instance);
        }