Пример #1
0
    //Añadir una sobrecarga de la fución spawnObject pasando el parametro de la rotación
    public static GameObject SpawnObject(GameObject prefab, PoolTypes type, Vector2 position, Quaternion rotation)
    {
        GameObject spawnObject = SpawnObject(prefab, type, position);

        spawnObject.transform.rotation = rotation;
        return(spawnObject);
    }
Пример #2
0
    public void ReturnToPool(PoolTypes type, GameObject toEnqueue /*, bool setDefaultParent = true*/)
    {
        poolDictionary[type].Enqueue(toEnqueue); // Vrátíme zase na začátek řady

        poolParents.TryGetValue(type, out GameObject pool_parent);
        if (pool_parent != null)
        {
            toEnqueue.transform.SetParent(pool_parent.transform);
        }
    }
Пример #3
0
    /// <summary> Gets the specified recycle pool </summary>
    /// <param name="_type"> Pool type from enum </param>
    /// <returns> The requested Stack </returns>
    static Stack <GameObject> GetStack(PoolTypes _type)
    {
        // First time? Create all stacks
        if (pools == null)
        {
            int poolCount = System.Enum.GetValues(typeof(PoolTypes)).Length;
            pools = new Stack <GameObject> [poolCount];
            for (int type = 0; type < poolCount; ++type)
            {
                pools[type] = new Stack <GameObject>();
            }
        }

        return(pools[(int)_type]);
    }
Пример #4
0
    public static GameObject SpawnObject(GameObject prefab, PoolTypes type, Vector2 position)
    {
        //Buscamos un objeto de ese tipo en el pool
        GameObject spawnObject = PoolSystem.GetElement(type);

        //Si no hay ninguno lo creamos
        if (spawnObject == null)
        {
            spawnObject = Instantiate(prefab);
        }

        //Lo posicionamos en el spwnpoint y lo activamos
        spawnObject.transform.position = position;
        spawnObject.SetActive(true);
        return(spawnObject);
    }
Пример #5
0
    /// <summary> Retrieves (if found), else Instantiates, the specified GameObject </summary>
    /// <param name="_type"> Pool type </param>
    /// <param name="_prefab"> Prefab to Instantiate from </param>
    /// <returns> The GameObject retrieved or created </returns>
    public static GameObject RetrieveOrCreate(PoolTypes _type, GameObject _prefab)
    {
        // Pop from stack if non-empty, else instantiate
        GameObject         gameObj;
        Stack <GameObject> stack = GetStack(_type);

        if (stack.Count != 0)
        {
            gameObj = stack.Pop();
        }
        else
        {
            gameObj = Object.Instantiate(_prefab);
        }

        gameObj.SetActive(true);

        return(gameObj);
    }
Пример #6
0
    public GameObject AddToPool(PoolTypes type)
    {
        foreach (PoolObjectSO poolData in poolDatas)
        {
            if (poolData.poolType == type && poolData.canExpand == true)
            {
                if (!poolData.canExpand)
                {
                    Debug.LogError("Pole u poolu objektů je potřeba expandovat, nastavení objektu akci však nepovoluje");
                    return(null);
                }

                GameObject obj = GameObject.Instantiate(poolData.objectToPool);
                poolDictionary[type].Enqueue(obj);
                return(obj);
            }
        }
        return(null);
    }
Пример #7
0
    static GameObject GetElement(PoolTypes type)
    {
        //si es la primera vez que se ejecuta se inicializa
        if (pooling.Count == 0)
        {
            Initialize();
        }

        //Si hay algún elemento de ese tipo lo combrueba, lo activa y lo devuelve
        if (pooling [type].Count > 0)
        {
            GameObject element = pooling [type] [0];
            pooling [type].Remove(element);
            element.SetActive(true);
            return(element);
        }

        return(null);
    }
Пример #8
0
    public GameObject GetFromPool(PoolTypes type, Vector3 position, Quaternion rotation, Transform parent = null)
    {
        parent = null;
        GameObject pooledObj;

        if (poolDictionary[type].Count > 0)
        {
            pooledObj = poolDictionary[type].Dequeue(); // Získáme poslední objekt z řady
        }
        else
        { //Jinak musíme do polu přidat nový
            pooledObj = AddToPool(type);

            if (poolDictionary[type].Count > 0)
            {
                pooledObj = poolDictionary[type].Dequeue();
            }
        }

        if (parent == null)
        {
            poolParents.TryGetValue(type, out GameObject pool_parent);
            if (pool_parent != null)
            {
                pooledObj.transform.SetParent(pool_parent.transform);
            }
        }
        else
        {
            pooledObj.transform.SetParent(parent);
        }


        pooledObj.transform.position = position;
        pooledObj.transform.rotation = rotation;

        pooledObj.SetActive(false);

        return(pooledObj);
    }
Пример #9
0
    public GameObject BirthGameObject(PoolTypes poolType, Vector3 pos, Quaternion rot)
    {
        GameObject obj = null;

        for (int i = 0; i < poolsArray.Count; i++)
        {
            PoolOption option = this.poolsArray[i];
            if (option.poolType == poolType)
            {
                obj = option.Active(pos, rot);
                if (obj == null)
                {
                    return(null);
                }
                if (obj.transform.parent != ThisGameObjectPosition)
                {
                    obj.transform.parent = ThisGameObjectPosition;
                }
            }
        }
        return(obj);
    }
Пример #10
0
 /// <summary> Recycle a GameObject to its pool </summary>
 /// <param name="_type"> Pool type </param>
 /// <param name="_gameObj"> GameObject to recycle </param>
 public static void Recycle(PoolTypes _type, GameObject _gameObj)
 {
     _gameObj.SetActive(false);
     GetStack(_type).Push(_gameObj);
 }
Пример #11
0
 public static void AddElementToPool(PoolTypes type, GameObject element)
 {
     //el objecto se añade al pool y se desactiva
     pooling [type].Add(element);
     element.SetActive(false);
 }