/// <summary> /// 建立对象池,对象池通过框架类的类型绑定 /// </summary> /// <param name="type">对象绑定的框架类类型</param> /// <param name="path">要建池的对象</param> public void AddPool(System.Type type, GameObject gameObject) { if (PoolDic.ContainsKey(type)) { Log.Warning("类型{0},已经被建立过对象池了,不要重复建立.", type.ToString()); return; } //1 在这个节点底下创建一个节点,用于保存未激活的池对象 Transform node = new GameObject(gameObject.name).transform; node.SetParent(poolRootTransform); node.localPosition = Vector3.zero; node.gameObject.SetActive(false); //2 新建对象池 SinglePool pool = new SinglePool(type, gameObject, node, this); PoolDic.Add(type, pool); //3 添加新的反向绑定词典 data.Add(type, new Dictionary <GameObject, object>()); }
/// <summary> /// 缓存对象 /// </summary> /// <param name="key">类型</param> /// <returns>缓存对象</returns> public Tvalue this[Tkey key] { get { if (pool.ContainsKey(key)) { Tvalue t = pool[key].Get(); t.DestroyedEvent += AliveDestroyed; aliveList.Add(t); return(t); } return(default(Tvalue)); } set { if (aliveList.Contains(value)) { aliveList.Remove(value); } if (value == null) { return; } if (pool.ContainsKey(key)) { value.DestroyedEvent -= AliveDestroyed; pool[key].Set(value); } else { SinglePool <Tvalue> p = new SinglePool <Tvalue>(); p.SetTempleAndCount(value, cacheCountDic.ContainsKey(key) ? cacheCountDic[key] : 1); pool.Add(key, p); } } }