Example #1
0
    public void MakeObjectPool()
    {
        for (int i = 0; i < foodManager.foodDatas.Length; i++)
        {
            for (int j = 0; j < _loadingNumOfOnePrefab; j++)
            {
                GameObject foodObj = new GameObject();

                Food newFood = foodObj.AddComponent <Food>();
                newFood = foodManager.FillFoodInfoByLevel(i, newFood);

                SpriteRenderer renderer = foodObj.AddComponent <SpriteRenderer>();
                renderer.sprite = newFood.sprite;

                foodObj.AddComponent <BoxCollider2D>();

                foodObj.SetActive(false);
                foodObj.name          = newFood.name;
                foodObj.transform.tag = "Food";

                _foodObjPool.Add(foodObj);
            }
        }

        //shuffle List
        List <int> shuffleList = new List <int>();

        shuffleList.Capacity = _foodObjPool.Count;
        for (int id = 0; id < _foodObjPool.Count; id++)
        {
            int ranid = UnityEngine.Random.Range(0, id);
            shuffleList.Insert(ranid, id);
        }

        for (int i = 0; i < _foodObjPool.Count; i++)
        {
            _notMovingFoodIndexQueue.Enqueue(shuffleList[i]);
        }
    }