Ejemplo n.º 1
0
    /// <summary>
    /// 创建池
    /// </summary>
    private void expandPool()
    {
        int start = _startCapacityIndex;
        int end   = _startCapacityIndex + _initialCapacity;

        for (int i = start; i < end; i++)
        {
            //加入验证判断,避免在极端情况下

            //(1:两次申请同时发起 2:第二次申请的时候,第一次还没有扩展任何元素)时

            if (_totalObjList.ContainsKey(i))
            {
                continue;
            }

            GameObject newObj = Instantiate(objPrefab) as GameObject;

            newObj.AddComponent <SkillDispose>();

            SkillDispose skillScript = newObj.GetComponent <SkillDispose>();
            skillScript.id = i;

            newObj.SetActive(false);

            _avaliableIndex.Add(i);

            _totalObjList.Add(i, newObj);
        }

        // 更新索引
        _startCapacityIndex = end;
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 取得一个物体,返回值 1,obj代表,ID是1的物体被取到,ID可以用来归还物体的时候用到
    /// </summary>
    /// <returns></returns>
    public KeyValuePair <int, GameObject> pickObj()
    {
        if (_avaliableIndex.Count == 0)
        {
            expandPool();
        }

        int id = _avaliableIndex[0];

        _avaliableIndex.Remove(id);

        _totalObjList[id].SetActive(true);

        SkillDispose skillScript = _totalObjList[id].GetComponent <SkillDispose>();

        // 重置技能时间
        skillScript.time = survivalTime;

        return(new KeyValuePair <int, GameObject>(id, _totalObjList[id]));
    }