Example #1
0
    public GameObject GetObject(PoolType id)
    {
        var obj = _pools[(int)id].GetObject();

        if (obj == null)
        {
            Debug.LogFormat("Pool {0} is empty.", id);

            if (DynamicPool)
            {
                obj = Instantiate(_pools[(int)id].OriginalPrefabe());
                IPoollable IPoollabl = obj.GetComponent <IPoollable>();
                if (IPoollabl != null)
                {
                    IPoollabl.Init();
                }

                Debug.LogFormat("Add one object to {0} pool.", id);
            }
        }

        IPoollable iPoollable = obj.GetComponent <IPoollable>();

        if (iPoollable != null)
        {
            iPoollable.ReSpawn();
        }

        return(obj);
    }
Example #2
0
    public Pool(int id, int amount, Object prefabe, Transform parent = null)
    {
        _cashedStack    = new Stack <object>();
        CommonTransform = new GameObject(Enum.GetName(typeof(PoolType), id) + "_POOL").transform;
        _idPool         = id;
        _originalObj    = prefabe;

        for (int i = 0; i < amount; i++)
        {
            GameObject go = Object.Instantiate(prefabe, Vector3.zero, Quaternion.identity, CommonTransform) as GameObject;
            _cashedStack.Push(go);
            go.SetActive(false);

            IPoollable ipoolable = go.GetComponent <IPoollable>();
            if (ipoolable != null)
            {
                ipoolable.Init();
            }
        }

        if (parent != null)
        {
            CommonTransform.SetParent(parent);
        }
    }
Example #3
0
    // public void

    /// <summary>
    /// добавить объект к уже существующему пулу
    /// </summary>
    /// <param name="id"></param>
    /// <param name=""></param>
    public void AddObject(PoolType id, GameObject prefab, int count)
    {
        Pool pool = null;

        if (_pools.TryGetValue((int)id, out pool))
        {
            for (int i = 0; i < count; i++)
            {
                var obj = Instantiate(prefab, transform);
                obj.SetActive(false);
                IPoollable ipoolable = obj.GetComponent <IPoollable>();
                if (ipoolable != null)
                {
                    ipoolable.Init();
                }

                pool.AddObject(obj, true);
            }
        }

        else
        {
            Debug.Log("Pool is not find");
        }
    }
Example #4
0
 internal void RetrieveCollectable(IPoollable _collectable)
 {
     if (!avaiableObjects.Contains(_collectable))
     {
         _collectable.RetrieveObject(pooledData.ObjectStateOnRetrieve, parentTransform);
         avaiableObjects.Add(_collectable);
         unavaiableObjects.Remove(_collectable);
     }
 }
Example #5
0
 /// <summary>
 /// Per ogni struttura crea i prefab degli oggetti e se li salva in una lista
 /// </summary>
 void PopulatePool()
 {
     for (int i = 0; i < pooledData.Quantity; i++)
     {
         IPoollable p = InstantiateCollectables();
         avaiableObjects.Add(p);
         p.ToggleObject(false);
     }
 }
Example #6
0
    /// <summary>
    /// Istanzia i collectable
    /// </summary>
    /// <returns></returns>
    IPoollable InstantiateCollectables()
    {
        IPoollable p = null;

        p = GameObject.Instantiate(pooledData.Prefab, parentTransform).GetComponent <IPoollable>();
        if (p != null)
        {
            p.Setup(pooledData.ID);
        }
        else
        {
            throw new System.Exception("**Poollable not found on the prefab**");
        }
        return(p);
    }
Example #7
0
    /// <summary>
    /// Controlla se è presente un collectable disponibile, lo sposta dalla lista dei disponibili a quella dei non disponibili,
    /// e lo restituisce. (Se la lista è a zero, crea un nuovo collectable)
    /// </summary>
    /// <returns></returns>
    public T GetFirstCollectable()
    {
        IPoollable obj = null;

        if (avaiableObjects.Count > 0)
        {
            obj = avaiableObjects[0];
            unavaiableObjects.Add(obj);
            avaiableObjects.Remove(obj);
        }
        else
        {
            obj = InstantiateCollectables();
        }

        obj.ToggleObject(true);
        return((T)obj);
    }
Example #8
0
    public Pool(int id, int amount, GameObject prefabe, Transform parent, bool createCommonTrans)
    {
        _cashedStack = new Stack <object>();
        _idPool      = id;
        _originalObj = prefabe;

        for (int i = 0; i < amount; i++)
        {
            var go = Object.Instantiate(prefabe, Vector3.zero, Quaternion.identity, parent);
            _cashedStack.Push(go);
            IPoollable ipoolable = go.GetComponent <IPoollable>();
            if (ipoolable != null)
            {
                ipoolable.Init();
            }
            go.SetActive(false);
        }
    }