Exemple #1
0
 public ObjectPoolData(ObjectPoolName name, APoolable prefab, int initialCapacity, RootBase root)
 {
     this.name            = name;
     this.prefab          = prefab;
     this.initialCapacity = initialCapacity;
     this.Root            = root;
 }
    //---------------------------------------------------------------------------------------------------------------
    public virtual void Push(APoolable item)
    {
#if DEBUG
        if (item == null || item.gameObject == null)
        {
            return;
        }

        if (stack.Contains(item))
        {
            Debug.LogError("Tried to pool already pooled object. Ignoring...Check for duplicate return to pool " + data.name);
            return;
        }
        if (!item.gameObject.activeSelf)
        {
            Debug.LogError("Tried to pool inactive object. Ignoring...Check for duplicate return to pool " + data.name);
            return;
        }

        if (itemsInUse.Count < 1)
        {
            Debug.LogError("Tried to pool object while pool had no items in use. Pool: " + data.name);
            return;
        }
#endif

        item.SetPooledStatus();
        item.gameObject.SetActive(false);
        stack.Push(item);
        itemsInUse.Remove(item);


        item.transform.SetParent(parent.transform);
    }
Exemple #3
0
    public void ReleasePoolable(APoolable poolableObject)
    {
        this.usedObjects.Remove(poolableObject);

        poolableObject.Release();
        poolableObject.gameObject.SetActive(false);
        this.availableObjects.Add(poolableObject);
    }
Exemple #4
0
    //---------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// Works as "Instantiate" for standard prefab, but instead moves poolable Object from the container or creates new if there is no more poolable objects left.
    /// You need to clean the object for future use in OnReturnedToPool OR OnPop.
    /// </summary>
    public APoolable Pop(ObjectPoolName name, Transform NewParent)
    {
        APoolable result = this.Pop(name);

        result.gameObject.SetActive(true);
        result.transform.SetParent(NewParent);

        return(result);
    }
Exemple #5
0
    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("[BoundaryHandler] Collision detected: " + collision.gameObject.name);
        APoolable poolableObject = collision.gameObject.GetComponent <APoolable> ();

        if (this.boundaryListener != null)
        {
            this.boundaryListener.OnExitBoundary(poolableObject);
        }
    }
Exemple #6
0
 public void Initialize()
 {
     for (int i = 0; i < this.maxPoolSize; i++)
     {
         APoolable poolableObject = GameObject.Instantiate <APoolable> (this.poolableObjectCopy, this.poolableParent);
         poolableObject.Initialize();
         poolableObject.gameObject.SetActive(false);
         this.availableObjects.Add(poolableObject);
     }
 }
Exemple #7
0
    //---------------------------------------------------------------------------------------------------------------
    public static bool IsNullOrPooled(this APoolable ap)
    {
        if (ap == null)
        {
            return(true);
        }
        if (ap.IsPooled)
        {
            return(true);
        }

        return(false);
    }
    private void OnPoolableAudioSourceReturned(APoolable source, Transform parent)
    {
        var poolableAudioSource = source as PoolableAudioSource;

        if (poolableAudioSource == null)
        {
            return;
        }
        if (playingCache.TryGetValue(parent, out var sources))
        {
            sources.Remove(poolableAudioSource);
        }
    }
    //---------------------------------------------------------------------------------------------------------------
    void AddInstance()
    {
        APoolable item = (APoolable)MonoBehaviour.Instantiate(data.prefab, parent.transform);

        item.OnCreate();
        item.gameObject.SetActive(false);
        item.SetPooledStatus(); // Here was a bug: AddInstance and Pop do the same thing with semi-duplicated code, so AddInstance didn't set Pooled status to new Instances. TODO: Unify duplicated code.
        item.Pool = this;
        stack.Push(item);

#if UNITY_EDITOR
        item.name = data.prefab.name + stack.Count;
#endif
    }
    //---------------------------------------------------------------------------------------------------------------
    public virtual APoolable Pop()
    {
        if (stack.Count == 0)
        {
            AddInstance();
        }


        APoolable item = stack.Pop();

        item.SetPopedStatus();
        item.OnPop();

        itemsInUse.Add(item);
        return(item);
    }
Exemple #11
0
    public APoolable RequestPoolable()
    {
        if (this.HasObjectAvailable(1))
        {
            APoolable poolableObject = this.availableObjects [this.availableObjects.Count - 1];
            this.availableObjects.RemoveAt(this.availableObjects.Count - 1);
            this.usedObjects.Add(poolableObject);

            poolableObject.gameObject.SetActive(true);
            poolableObject.OnActivate();
            return(poolableObject);
        }
        else
        {
            Debug.LogError("[GameObjectPool] No more poolable object available!");
            return(null);
        }
    }
Exemple #12
0
    public APoolable[] RequestPoolableBatch(int size)
    {
        if (this.HasObjectAvailable(size))
        {
            APoolable[] poolableObjects = new APoolable[size];

            for (int i = 0; i < size; i++)
            {
                poolableObjects [i] = this.RequestPoolable();
            }

            return(poolableObjects);
        }
        else
        {
            Debug.LogError("[GameObjectPool] Insufficient objects available in pool. Count is: " + this.availableObjects.Count + " while requested is " + size + "!");
            return(null);
        }
    }
Exemple #13
0
 public void OnExitBoundary(APoolable poolableObject)
 {
     this.objectPool.ReleasePoolable(poolableObject);
 }