资源池初接触

资源池

一个简单的资源池创建,需要有如下功能:

  1. 调用激活
  2. 验证容量选择存储或销毁
  3. 重置position或初始化

首先是初始化资源池的结构,也就是搭建资源池容器

	public enum EffectType
		{
		    None,
		    SmokeEffect,
		    UncoveredEffect,
		    GoldEffect
		}
	//存放某类型的游戏物体list
	Dictionary<EffectType, List<GameObject>> poolListDic = new Dictionary<EffectType, List<GameObject>>();
	//三种游戏物体
    List<GameObject> uncoveredEffectList = new List<GameObject>();
    List<GameObject> smokeEffectList = new List<GameObject>();
    List<GameObject> goldEffectList = new List<GameObject>();
    //存放某类型的游戏物体最多数量list
    Dictionary<EffectType, int> poolListCapacityDic = new Dictionary<EffectType, int>();
    int uncoveredEffectListCapacity;
    int smokeEffectListCapacity = 5;
    int goldEffectListCapacity = 20;
    //存放某类型的游戏物体
    Dictionary<EffectType, GameObject> effectGoDic = new Dictionary<EffectType, GameObject>();
    //在awake里赋值
	private void Awake()
    {
        _instance = this;
        poolListDic.Add(EffectType.UncoveredEffect, uncoveredEffectList);
        poolListDic.Add(EffectType.SmokeEffect, smokeEffectList);
        poolListDic.Add(EffectType.GoldEffect, goldEffectList);
        
        uncoveredEffectListCapacity = (int)(GameManager.Instance.w * GameManager.Instance.h * 0.2f);
        
        poolListCapacityDic.Add(EffectType.UncoveredEffect, uncoveredEffectListCapacity);
        poolListCapacityDic.Add(EffectType.SmokeEffect, smokeEffectListCapacity);
        poolListCapacityDic.Add(EffectType.GoldEffect, goldEffectListCapacity);
        effectGoDic.Add(EffectType.UncoveredEffect, GameManager.Instance.uncoveredEffect);
        effectGoDic.Add(EffectType.SmokeEffect, GameManager.Instance.smokeEffect);
        effectGoDic.Add(EffectType.GoldEffect, GameManager.Instance.goldEffect);
    }

激活(取得资源池里对应的游戏物体)

public GameObject GetInstance(EffectType type, Transform t = null, bool worldPosStays = false)
    {
        List<GameObject> list;
        poolListDic.TryGetValue(type, out list);
        if (list.Count > 0)
        {
            GameObject tempGo = list[list.Count - 1];
            tempGo.SetActive(true);
            ResetInstance(tempGo);
            if (t != null)
            {
                tempGo.transform.SetParent(t, worldPosStays);
            }
            list.RemoveAt(list.Count - 1);//因为是同一个枚举类型,所以取得这个枚举类型对应的游戏物体
            return tempGo;
        }
        else
        {
            GameObject go;
            effectGoDic.TryGetValue(type, out go);
            if (t != null)
            {
                return Instantiate(go, t, worldPosStays);
            }
            return Instantiate(go);
        }
    }

重置(此代码段里为重置粒子系统)

private void ResetInstance(GameObject go)
    {
        ParticleSystem ps = go.GetComponent<ParticleSystem>();
        if (ps != null)
        {
            ps.Stop();
            ps.Play();
        }
        foreach (Transform t in go.transform)
        {
            ParticleSystem tps = go.GetComponent<ParticleSystem>();
            if (tps != null)
            {
                tps.Stop();
                tps.Play();
            }
        }
    }

存储

public void StoreInstance(EffectType type,GameObject go)
    {
        List<GameObject> list;
        poolListDic.TryGetValue(type, out list);
        int listCap;
        poolListCapacityDic.TryGetValue(type, out listCap);
        if (list.Count < listCap)//如果池子里对应枚举类型的list没有超过最大值,就加入该list,并取消激活状态
        {
            go.SetActive(false);
            list.Add(go);
        }
        else
        {
            Destroy(go);//反之不加入,直接销毁
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值