/// <summary>
    /// the function for spawn the item of GameObject pool
    /// </summary>
    public GameObject SpawnGameObjectPoolItem(Vector3 position, Quaternion rotation)
    {
        //If found, remove it from the object pool
        if (m_GameObjectQueue.Count != 0)
        {
            // gameObject are available; It's just reinitializing
            GameObject gameObjectInGameObjectPool = m_GameObjectQueue.Dequeue();

            GameObjectPoolItem gameObjectPoolItem = gameObjectInGameObjectPool.GetComponent <GameObjectPoolItem>();

            gameObjectPoolItem.Reuse(position, rotation);

            return(gameObjectInGameObjectPool);
        }
        //If not found, to generate the new gameObject
        else
        {
            GameObject gameObjectClone = Instantiate(this.m_GameObjectPrefab, position, rotation);

            m_HitstoryCounter++;

            string gameObjectCloneName = string.Format("{0}{1}", gameObjectClone.name, m_HitstoryCounter.ToString());

            GameObjectPoolItem gameObjectPoolItem = gameObjectClone.GetComponent <GameObjectPoolItem>();

            gameObjectPoolItem.Initialize(gameObjectCloneName, this.gameObject.transform, this);

            gameObjectPoolItem.Reuse(position, rotation);

            return(gameObjectClone);
        }
    }
    /// <summary>
    /// the function for initializing the GameObject pool
    /// </summary>
    /// <param name="preloadAmount">The number of pregenerated gameObjects.</param>
    /// <param name="gameObjectPrefab">the GameObject asset that will be clone.</param>
    public void GameObjectPoolInitialize(GameObject gameObjectPrefab, int preloadAmount)
    {
        if (m_IsInitialized == true)
        {
            Debug.LogError("GameObjectPool has already been initialized !");

            return;
        }

        m_GameObjectQueue = new Queue <GameObject>(preloadAmount);

        this.m_GameObjectPrefab = gameObjectPrefab;

        for (int i = 0; i < preloadAmount; i++)
        {
            GameObject gameObjectClone = Instantiate(this.m_GameObjectPrefab);

            m_HitstoryCounter++;

            string gameObjectCloneName = string.Format("{0}{1}", gameObjectClone.name, m_HitstoryCounter.ToString());

            GameObjectPoolItem gameObjectPoolItem = gameObjectClone.GetComponent <GameObjectPoolItem>();

            gameObjectPoolItem.Initialize(gameObjectCloneName, this.gameObject.transform, this);

            m_GameObjectQueue.Enqueue(gameObjectClone);
        }

        m_IsInitialized = true;
    }
Exemple #3
0
    /// <summary>
    /// Destroy the specific gameObject pool
    /// </summary>
    /// <param name="gameObjectPoolName"></param>
    public static void DestroyGameObjectPool(string gameObjectPoolName)
    {
        GameObjectPool gameObjectPool = GetGameObjectPool(gameObjectPoolName);

        if (gameObjectPool == null)
        {
            Debug.LogErrorFormat("The gameObjectPool has already been destroy which name is : {0}", gameObjectPoolName);
        }

        GameObjectPoolItem[] gameObjectPoolItems = GameObject.FindObjectsOfType <GameObjectPoolItem>();

        for (int i = 0; i < gameObjectPoolItems.Length; i++)
        {
            GameObjectPoolItem gameObjectPoolItem = gameObjectPoolItems[i];

            if ((gameObjectPoolItem.GameObjectPool == gameObjectPool) && (gameObjectPoolItem.IsInGameObjectPool == false))
            {
                gameObjectPoolItem.Recycle();
            }
        }

        GameObject gameObjectPoolCarrier = gameObjectPool.gameObject;

        gameObjectPool.GameObjectPoolOnDestroy();

        UnRegistGameObjectPoolFromGameObjectPoolsDictionary(gameObjectPoolName);

        MonoBehaviour.Destroy(gameObjectPool);

        MonoBehaviour.Destroy(gameObjectPoolCarrier);
    }
Exemple #4
0
    public static void DestroyAllGameObjectPool()
    {
        GameObjectPoolItem[] gameObjectPoolItems = GameObject.FindObjectsOfType <GameObjectPoolItem>();

        for (int i = 0; i < gameObjectPoolItems.Length; i++)
        {
            GameObjectPoolItem gameObjectPoolItem = gameObjectPoolItems[i];

            if (gameObjectPoolItem.IsInGameObjectPool == false)
            {
                gameObjectPoolItem.Recycle();
            }
        }


        foreach (KeyValuePair <string, GameObjectPool> kv in m_GameObjectPoolsDictionary)
        {
            GameObjectPool gameObjectPool = kv.Value;

            if (gameObjectPool != null)
            {
                gameObjectPool.GameObjectPoolOnDestroy();

                GameObject gameObjectPoolCarrier = gameObjectPool.gameObject;

                MonoBehaviour.Destroy(gameObjectPool);

                MonoBehaviour.Destroy(gameObjectPoolCarrier);
            }
        }

        m_GameObjectPoolsDictionary.Clear();
    }
Exemple #5
0
    public void Release(GameObject o)
    {
        o.SetActive(false);
        o.transform.SetParent(transform, false);
        GameObjectPoolItem rl = o.GetComponent <GameObjectPoolItem>();

        if (rl != null)
        {
            rl.Release();
        }
        caches.Enqueue(o);
    }
Exemple #6
0
    public void Awake()
    {
        for (int i = 0; i < bulletImpactDataArray.Length; i++)
        {
            BulletImpactData bulletImpactData = bulletImpactDataArray[i];

            string             bulletImpactPoolName = bulletImpactData.bulletImpactPoolName;
            GameObjectPoolItem bulletImpactPoolItem = bulletImpactData.bulletImpactPoolItem;
            int bulletImpactPreNum = bulletImpactData.bulletImpactPreNum;

            if (GameObjectPoolManager.HasGameObjectPool(bulletImpactPoolName) == false)
            {
                GameObjectPoolManager.CreateGameObjectPoolForGameObject(bulletImpactPoolName, bulletImpactPoolItem.gameObject, bulletImpactPreNum);
            }

            GameObjectPool bulletImpactPool = GameObjectPoolManager.GetGameObjectPool(bulletImpactPoolName);
            m_ImpactPoolDictionary.Add(bulletImpactPoolItem.gameObject.layer, bulletImpactPool);
        }
    }
Exemple #7
0
    void Awake()
    {
        m_Rigidbody = GetComponent <Rigidbody>();

        m_AudioSource = GetComponent <AudioSource>();

        if (GameObjectPoolManager.HasGameObjectPool(trailEffectInfo.effectPoolName) == false)
        {
            GameObjectPoolManager.CreateGameObjectPoolForGameObject(trailEffectInfo.effectPoolName, trailEffectInfo.effectItem.gameObject, trailEffectInfo.effectPoolNum);
        }

        trailEffectInfo.effectPool = GameObjectPoolManager.GetGameObjectPool(trailEffectInfo.effectPoolName);


        for (int i = 0; i < hitExplosionDataArray.Length; i++)
        {
            EffectInfo missileExplosionData = hitExplosionDataArray[i];

            string             missileExplosionPoolName = missileExplosionData.effectPoolName;
            GameObjectPoolItem missileExplosionPoolItem = missileExplosionData.effectItem;
            int missileExplosionPreNum = missileExplosionData.effectPoolNum;

            if (GameObjectPoolManager.HasGameObjectPool(missileExplosionPoolName) == false)
            {
                GameObjectPoolManager.CreateGameObjectPoolForGameObject(missileExplosionPoolName, missileExplosionPoolItem.gameObject, missileExplosionPreNum);
            }

            GameObjectPool missileExplosionPool = GameObjectPoolManager.GetGameObjectPool(missileExplosionPoolName);
            hitExplosionDataArray[i].effectPool = missileExplosionPool;
            m_MissileExplosionPoolDictionary.Add(missileExplosionPoolItem.gameObject.layer, missileExplosionPool);
        }

        if (GameObjectPoolManager.HasGameObjectPool(spaceExplosionEffectInfo.effectPoolName) == false)
        {
            GameObjectPoolManager.CreateGameObjectPoolForGameObject(spaceExplosionEffectInfo.effectPoolName, spaceExplosionEffectInfo.effectItem.gameObject, spaceExplosionEffectInfo.effectPoolNum);
        }

        spaceExplosionEffectInfo.effectPool = GameObjectPoolManager.GetGameObjectPool(spaceExplosionEffectInfo.effectPoolName);
    }