public GameObject GetInstance(Vector3 position, bool expandPoolIfEmpty = true)
    {
        PoolablePrefab instanceHandle = GetInstanceHandle(position, expandPoolIfEmpty);

        if (!instanceHandle)
        {
            return(null);
        }
        return(instanceHandle.gameObject);
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Moves oldest element in queue into sleeping objects queue, resets binding to the pool's container and deactivates an object
        /// </summary>
        /// <param name="poolablePrefab"> Type of element in queue that need to moved. Type is matching with prefab's path in resources
        /// </param>
        private static void PushActiveItemToSleepItemsQueue(PoolablePrefab poolablePrefab)
        {
            var tQueueItem = poolablePrefab.activeGameObjects.Dequeue();

            if (tQueueItem.gameObject == null)
            {
                return;
            }

            tQueueItem.gameObject.SetActive(false);
            tQueueItem.gameObject.transform.localScale = Vector3.one;
            tQueueItem.gameObject.transform.parent     = _container;
            poolablePrefab.sleepingGameObjects.Enqueue(tQueueItem);
        }
    public bool RemoveInstanceHandleOf(GameObject prefab, PoolablePrefab instanceHandle)
    {
        if (!instanceHandle)
        {
            return(false);
        }
        PrefabPool pool = GetPool(prefab);

        if (pool == null)
        {
            return(false);
        }
        return(pool.RemoveInstance(instanceHandle.gameObject));
    }
    public bool StoreInstanceHandleOf(int prefabId, PoolablePrefab instanceHandle)
    {
        if (!instanceHandle)
        {
            return(false);
        }
        PrefabPool pool = GetPool(prefabId);

        if (pool == null)
        {
            return(false);
        }
        return(pool.StoreInstance(instanceHandle.gameObject));
    }
    public void Initialize(GameObject prefab = null, int initialSize = -1, int growAmount = -1, int maxSize = -1)
    {
        if (!PrefabPoolManager.Manager)
        {
            Debug.LogError("PrefabPoolManager instance does not exist. Cannot create PrefabPool instance.");
            return;
        }

        if (prefab)
        {
            this.prefab = prefab;
        }

        PoolablePrefab poolHandle = this.prefab.GetComponent <PoolablePrefab>() as PoolablePrefab;

        if (!poolHandle)
        {
            poolHandle = this.prefab.AddComponent <PoolablePrefab>() as PoolablePrefab;
        }

        if (poolHandle)
        {
            poolHandle.Prefab = this.prefab;
        }
        else
        {
            Debug.Log("Could not locate or add PoolablePrefab component on the supplied prefab:\n" + this.prefab);
        }

        Root = new GameObject(Prefab.name + "(Pool)");
        Root.transform.parent = PrefabPoolManager.Manager.gameObject.transform;

        if (initialSize >= 0)
        {
            this.initialSize = initialSize;
        }

        if (growAmount >= 0)
        {
            this.growAmount = growAmount;
        }

        if (maxSize >= 0)
        {
            this.maxSize = maxSize;
        }

        CreateInstances(this.initialSize);
    }
    public PoolablePrefab GetInstanceHandle(Vector3 position, bool expandPoolIfEmpty = true)
    {
        lock (lawk) {
            if (pooled.Count == 0 && expandPoolIfEmpty)
            {
                // no free instances to grab from pool. create more.
                CreateInstances(growAmount);
            }

            if (pooled.Count == 0)
            {
                // still no free instances. cant return one
                return(null);
            }

            int            keyToUse = -1;
            bool           found = false, cleanPooled = false;
            PoolablePrefab poolablePrefab = null;
            HashSet <int>  keysToRemove   = null;
            // find the first pooled GameObject
            foreach (int key in pooled)
            {
                if (pool.ContainsKey(key))
                {
                    poolablePrefab = pool[key];
                    if (poolablePrefab)
                    {
                        if (!poolablePrefab.gameObject.activeSelf)
                        {
                            found    = true;
                            keyToUse = key;
                            break;
                        }
                    }
                }
                // couldnt use this pooled key for some reason, so it isnt valid to keep
                // it in the pooled HashSet. Remove it and warn that it was in there.
                if (!cleanPooled)
                {
                    keysToRemove = new HashSet <int>();
                    cleanPooled  = true;
                }
                keysToRemove.Add(key);
            }

            if (cleanPooled)
            {
                Debug.LogWarning(keysToRemove.Count + " invalid keys exist in '" + Prefab.name + "' pool.");
                foreach (int key in keysToRemove)
                {
                    // remove the invalid keys
                    pooled.Remove(key);
                }

                if (!found && expandPoolIfEmpty)
                {
                    // if we didnt find any and the pool had to be cleaned, try to create more.
                    // An unclean pooled HashSet can prevent growing the pool when it runs out.
                    CreateInstances(growAmount);

                    // call this method again, but without the automatic expansion
                    return(GetInstanceHandle(position));
                }
            }

            if (!found)
            {
                return(null);
            }

            // remove its key from the hashset
            pooled.Remove(keyToUse);

            poolablePrefab.pooledInstanceId = currPooledInstanceId;
            currPooledInstanceId++;
            currPooledInstanceId = currPooledInstanceId % 0x7FffFFffFFffFFff;

            GameObject obj = poolablePrefab.gameObject;
            obj.transform.parent   = null;
            obj.transform.position = position;
            obj.SetActive(true);
            return(poolablePrefab);
        }
    }