Exemplo n.º 1
0
    private GameObject InstantiateInactiveObject(ObjectPoolItem item)
    {
        GameObject obj = Instantiate(item.objectToPool);

        obj.SetActive(false);
        return(obj);
    }
Exemplo n.º 2
0
    // simple Singleton
    protected virtual void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }

        // create a new entry in the object pool
        if (explosionPrefab != null)
        {
            ObjectPoolItem explosionPoolItem = new ObjectPoolItem
            {
                objectToPool = explosionPrefab,
                amountToPool = poolSize,
                shouldExpand = true
            };

            // add to the current pool
            itemsToPool.Add(explosionPoolItem);
        }
    }
Exemplo n.º 3
0
        private void CreateBulletDecal(RaycastHit hit)
        {
            ObjectPoolItem decal = bulletDecalPool.Spawn(hit.point, Quaternion.identity);

            decal.transform.forward  = hit.normal;
            decal.transform.position = decal.transform.position + (decal.transform.forward * ANTIOVERLAP_SPACE); // Separates a litte amount of space to avoid overlap
        }
Exemplo n.º 4
0
 public void AddPoolItem(ObjectPoolItem item)
 {
     if (ItemsToPool != null)
     {
         ItemsToPool.Add(item);
     }
 }
Exemplo n.º 5
0
    private PoolBehaviour CreatePooledObject(ObjectPoolItem item)
    {
        // if (!Application.isPlaying && !item.inEditor)
        // {
        //     Debug.Log("not play not editor - " + item.objectToPool);
        //     return null;
        // }
        GameObject obj = Instantiate(item.objectToPool);
        // Get the parent for this pooled object and assign the new object to it
        var parentPoolObject = GetParentPoolObject(item.poolName);

        obj.transform.parent = parentPoolObject.transform;
        var poolBehaviour = obj.AddComponent <PoolBehaviour>();

        poolBehaviour.name = item.objectToPool.name;
#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            obj = PrefabUtility.ConnectGameObjectToPrefab(obj, item.objectToPool);
        }
#endif

        obj.SetActive(false);
        pooledObjects.Add(poolBehaviour);


        return(poolBehaviour);
    }
 public int AddObject(GameObject obj, int amt = 4, bool exp = true)
 {
     ObjectPoolItem item = new ObjectPoolItem(obj, amt, exp);
     int currLen = itemsToPool.Count;
     itemsToPool.Add(item);
     ObjectPoolItemToPooledObject(currLen);
     return currLen;
 }
Exemplo n.º 7
0
 public void AddItemToPool(GameObject type, ObjectPoolItem poolItem)
 {
     if (!PoolExists(type))
     {
         poolDict[type] = new List <ObjectPoolItem>();
     }
     poolDict[type].Add(poolItem);
 }
    public int AddObject(GameObject GO, int amt = 10, bool exp = true)
    {
        ObjectPoolItem item    = new ObjectPoolItem(GO, amt, exp);
        int            currLen = itemsToPool.Count;

        itemsToPool.Add(item);
        ObjectPoolItemToPooledObject(currLen);
        return(currLen);
    }
Exemplo n.º 9
0
    public static ObjectPoolItem newItem(int amountToPool, GameObject objectToPool, bool shouldExpand)
    {
        var newPoolItem = new ObjectPoolItem();

        newPoolItem.amountToPool = amountToPool;
        newPoolItem.objectToPool = objectToPool;
        newPoolItem.shouldExpand = shouldExpand;
        return(newPoolItem);
    }
Exemplo n.º 10
0
    private GameObject InstantiatePooledObject(ObjectPoolItem item)
    {
        GameObject obj = (GameObject)Instantiate(item.objectToPool);

        obj.SetActive(false);
        obj.transform.SetParent(pooledObjectsParent);
        pooledObjects.Add(obj);
        return(obj);
    }
Exemplo n.º 11
0
    private GameObject AddPooledObject(ObjectPoolItem item)
    {
        GameObject obj = (GameObject)Instantiate(item.objectToPool);

        obj.SetActive(false);
        obj.transform.parent = poolParent.Find("Pool_" + item.objectToPool.tag).transform;
        obj.GetComponent <PoolableObject>().poolParent = obj.transform.parent;
        pooledObjects.Add(obj);
        return(obj);
    }
Exemplo n.º 12
0
    public static ObjectPoolController InstantiatePool(int poolSize, ObjectPoolItem item, string poolName)
    {
        ObjectPoolController pool = Instantiate(
            Resources.Load <ObjectPoolController>("Prefabs/ObjectPool")
            );

        pool.name = poolName;
        pool.Init(poolSize, item);
        return(pool);
    }
Exemplo n.º 13
0
    private GameObject GetGameObject(ObjectPoolItem item)
    {
        item.objectToPool.SetActive(false);
        GameObject obj = Instantiate(item.objectToPool) as GameObject;

        obj.transform.parent = item.Pool.transform;
        obj.name             = item.objectToPool.name;
        obj.SetActive(false);
        pooledObjects.Add(obj);
        return(obj);
    }
Exemplo n.º 14
0
    public void AddGameObject(GameObject item)
    {
        ObjectPoolItem poolitem = new ObjectPoolItem();

        poolitem.AmountToPool = 1;
        poolitem.canExpand    = false;
        poolitem.objectToPool = item;
        if (ItemsToPool != null)
        {
            ItemsToPool.Add(poolitem);
        }
    }
    GameObject CreatePooledObject(ObjectPoolItem item, int sortIndex)
    {
        GameObject obj = Instantiate(item.obj) as GameObject;

        obj.transform.SetParent(GetSubParentPoolContainer(item.poolName).transform, false);
        //obj.GetComponent<SpriteRenderer>().sortingOrder += sortIndex;
        obj.transform.localScale = Vector3.zero;
        obj.SetActive(true);

        SortIntoLists(obj, obj.GetComponent <ObjectBase>().type);
        return(obj);
    }
Exemplo n.º 16
0
    private GameObject CreatePooledObject(ObjectPoolItem Item)
    {
        GameObject Obj = Instantiate <GameObject>(Item.ObjectToPool);

        // Get the parent for this pooled object and assign the new object to it
        var ParentPoolObject = GetParentPoolObject(Item.PoolName);

        Obj.transform.parent = ParentPoolObject.transform;

        Obj.SetActive(false);
        PooledObjects.Add(Obj);
        return(Obj);
    }
Exemplo n.º 17
0
    private GameObject createPooledObject(ObjectPoolItem item)
    {
        GameObject gameObj = Instantiate <GameObject>(item.objectToPool);

        GameObject parentPoolObject = getParentPoolObject(item.poolName);

        gameObj.transform.parent = parentPoolObject.transform;

        gameObj.SetActive(false);
        pooledObjects.Add(gameObj);

        return(gameObj);
    }
Exemplo n.º 18
0
    private GameObject CreatePooledObject(ObjectPoolItem item)
    {
        GameObject obj = Instantiate <GameObject>(item.objectToPool);

        // Get the parent for this pooled object and assign the new object to it
        var parentPoolObject = GetParentPoolObject(item.poolName);

        obj.transform.parent = parentPoolObject.transform;

        obj.SetActive(false);
        pooledObjects.Add(obj);
        return(obj);
    }
Exemplo n.º 19
0
    /**
     * Internal methods
     **/

    internal GameObject expandPool(ObjectPoolItem item)
    {
        print("Expanding pool by 1");
        string tag = item.objectToPool.tag;

        if (objectCount[tag] < item.maxPoolAmount || item.maxPoolAmount < 0)
        {
            objectCount[tag]++;
            GameObject newObj = Instantiate(item.objectToPool);
            newObj.SetActive(false);
            return(newObj);
        }
        return(null);
    }
Exemplo n.º 20
0
    public ObjectPoolItem CreateItemToPool(GameObject type)
    {
        ObjectPoolItem      thisPoolItem      = new ObjectPoolItem(Instantiate(type));
        ObjectPoolReference thisPoolReference = thisPoolItem.itemInstance.GetComponent <ObjectPoolReference>();

        if (thisPoolReference != null)
        {
            thisPoolReference.objectPool = this;
            thisPoolReference.objectType = type;
        }

        AddItemToPool(type, thisPoolItem);
        return(thisPoolItem);
    }
Exemplo n.º 21
0
    void ObjectPoolItemToPooledObject(int index)
    {
        ObjectPoolItem item = itemsToPool[index];

        pooledObjects = new List <GameObject>();
        for (int i = 0; i < item.amountToPool; i++)
        {
            GameObject obj = Instantiate(item.objectToPool, transform, false);
            obj.SetActive(false);
            pooledObjects.Add(obj);
        }
        pooledObjectsList.Add(pooledObjects);
        positions.Add(0);
    }
Exemplo n.º 22
0
    private void ObjectPoolItemToPooledObject(int index)
    {
        ObjectPoolItem item = itemsToPool[index];

        pooledObjects = new List<GameObject>();
        for (int i = 0; i < item.amountToPool; i++)
        {
            GameObject obj = (GameObject)GameObject.Instantiate(item.objectToPool);
            obj.SetActive(false);
            obj.transform.SetParent(parent.transform);
            pooledObjects.Add(obj);
        }
        pooledObjectsList.Add(pooledObjects);
        position.Add(0);
    }
Exemplo n.º 23
0
    void ObjectPoolItemToPooledObject(int index)
    {
        ObjectPoolItem item = itemsToPool[index];

        pooledObjects = new List <GameObject>();
        for (int i = 0; i < item.amountToPool; i++)
        {
            GameObject obj = (GameObject)Instantiate(item.objectToPool, Vector3.zero, Random.rotation);
            obj.SetActive(false);
            obj.transform.parent = this.transform;
            pooledObjects.Add(obj);
        }
        pooledObjectsList.Add(pooledObjects);
        positions.Add(0);
    }
Exemplo n.º 24
0
    /// <summary>
    /// Returns the oldest ObjectPoolItem in the pool and enables it.
    /// </summary>
    /// <returns>An ObjectPoolItem.</returns>
    public ObjectPoolItem Spawn()
    {
        ObjectPoolItem item = null;

        if (ObjectsAvailable())
        {
            item = _pool.Dequeue();
        }

        if (item != null)
        {
            item.Enable();
        }
        return(item);
    }
Exemplo n.º 25
0
    public List <GameObject> GetPooledObjects(int n, ObjectPoolTypeVariable type = null)
    {
        if (type == null)
        {
            type = pooledObjects.Keys.First();
        }

        List <GameObject> result = new List <GameObject>();
        int length = 0;

        foreach (GameObject pooledObject in pooledObjects[type])
        {
            if (!pooledObject.activeInHierarchy)
            {
                result.Add(pooledObject);
                length++;

                if (length == n)
                {
                    return(result);
                }
            }
        }

        // In case of expand
        ObjectPoolItem objectPoolItemToUse = null;

        foreach (ObjectPoolItem objectPoolItem in objectPoolItems)
        {
            if (type == null || objectPoolItem.Type.ToString() == type.ToString())
            {
                objectPoolItemToUse = objectPoolItem;
                break;
            }
        }
        foreach (Transform child in transform)
        {
            if (type == null || child.name == type.ToString())
            {
                while (length < n)
                {
                    result.Add(InstantiatePooledObject(objectPoolItemToUse, child));
                    length++;
                }
            }
        }
        return(null);
    }
Exemplo n.º 26
0
    private void ObjectPoolItemToPooledObject(int index)
    {
        ObjectPoolItem item = itemsToPool[index];

        pooledObjects = new List <GameObject>();
        for (int i = 0; i < item.amountToPool; i++)
        {
            spawnPosition = new Vector2(transform.position.x, transform.position.y + Random.Range(-4.0f, 3f));
            GameObject obj = (GameObject)Instantiate(item.objectToPool, spawnPosition, Quaternion.identity);
            obj.SetActive(false);

            pooledObjects.Add(obj);
        }
        pooledObjectsList.Add(pooledObjects);
        positions.Add(0);
    }
Exemplo n.º 27
0
    GameObject InstantiatePooledObject(ObjectPoolItem objectPoolItem, Transform parent)
    {
        GameObject pooledObject = Instantiate(objectPoolItem.ObjectToPool);

        pooledObject.transform.SetParent(parent.transform);
        if (isUI)
        {
            RectTransform rectTransform = pooledObject.GetComponent <RectTransform>();
            rectTransform.anchoredPosition = rectTransform.transform.position;
            rectTransform.localPosition    = objectPoolItem.ObjectToPool.transform.localPosition;
            rectTransform.localScale       = objectPoolItem.ObjectToPool.transform.localScale;
        }
        pooledObject.SetActive(false);
        pooledObject.name = objectPoolItem.Type.ToString();
        pooledObjects[objectPoolItem.Type].Add(pooledObject);
        return(pooledObject);
    }
Exemplo n.º 28
0
 bool CreateObjectPoolItem(GameObject go, out int instanceID, out ObjectPoolItem item)
 {
     instanceID = 0;
     item = null;
     
     var parameter = go.GetComponent<ObjectPoolParameter>();
     if (parameter == null)
         return false;
     
     instanceID = parameter.InstanceID;
     if (ObjectPoolItems.TryGetValue(instanceID, out item))
         return true;
     
     item = new ObjectPoolItem();
     ObjectPoolItems[instanceID] = item;
     return true;
 }
Exemplo n.º 29
0
    public void OptimizePoolList()
    {
        //Debug.Log("Pooling unoptimized: " + ItemsToPool.Count);
        //Debug.Log("Optimizing pool list");

        List <ObjectPoolItem> tempList = new List <ObjectPoolItem>();

        tempList.Clear();

        foreach (ObjectPoolItem objItem in ItemsToPool)
        {
            //Debug.Log("Checking poolitem: " + objItem.objectToPool.name);

            if (tempList.Exists(x => x.objectToPool == objItem.objectToPool))
            {
                ObjectPoolItem tempObj = tempList.Find(x => x.objectToPool == objItem.objectToPool);
                if (tempObj != null)
                {
                    //Debug.Log("Found, Adding: " + tempObj.objectToPool.name);
                    tempObj.AmountToPool += objItem.AmountToPool;
                    pooledItemsCount     += objItem.AmountToPool;
                }
            }
            else
            {
                //Debug.Log("!!Not found, Created: " + objItem.objectToPool.name);
                tempList.Add(objItem);
                pooledItemsCount += objItem.AmountToPool;
            }
        }

        ItemsToPool = tempList;
        //Debug.Log("Pooling optimized: " + ItemsToPool.Count);

        //Instantiate units

        if (pooledItemsCount > maxPoolItems)
        {
            ResizePoolItemList();
        }
        else
        {
            PoolObjects();
        }
    }
Exemplo n.º 30
0
    internal List <GameObject> expandPool(ObjectPoolItem item, int numExpand, List <GameObject> fillList)
    {
        print(string.Format("Expanding pool by {0}", numExpand));
        string tag = item.objectToPool.tag;
        // Find up to how many we can add
        int howMany = item.maxPoolAmount < 0? numExpand :
                      Mathf.Max(0, numExpand - Mathf.Max(0, objectCount[tag] + numExpand - item.maxPoolAmount));

        for (int i = 0; i < howMany; i++)
        {
            GameObject newObj = Instantiate(item.objectToPool);
            newObj.SetActive(false);
            fillList.Add(newObj);
        }
        objectCount[tag] += howMany;

        return(fillList);
    }
Exemplo n.º 31
0
    /// <summary>
    /// Create Pooled object and set parent
    /// </summary>
    /// <param name="item">Getting name inside of objects entity name parameter</param>
    public GameObject CreatePooledObject(ObjectPoolItem item)
    {
        GameObject obj = Instantiate <GameObject>(item.objectToPool);

        obj.name = item.poolName;
        // Get the parent for this pooled object and assign the new object to it
        var           parentPoolObject = GetParentPoolObject(item.poolName);
        RectTransform ui = obj.GetComponent <RectTransform>();

        if (ui == null)
        {
            obj.transform.parent = parentPoolObject.transform;
        }

        obj.SetActive(false);
        pooledObjects.Add(obj);
        return(obj);
    }