public SkillController(IObjectPool objectPool) { _pool = objectPool; var skills = Resources.LoadAll <SkillVFX>("VFX/Skills"); foreach (var skill in skills) { _pool.Add(skill.gameObject, InitCount); } }
/// <summary> /// 返回某个对象,如果不存在则调用函数委托获取值,并将值存入对象池后返回 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pool">对象池</param> /// <param name="key">键值</param> /// <param name="func">如果对象池不存在对应键的数据,则从此函数委托获取新值并存入对象池后返回</param> /// <returns></returns> public static T GetOrAdd <T>(this IObjectPool pool, object key, Func <T> func) { if (!pool.Contains(key)) { T value = func.Invoke(); pool.Add(key, value); return(value); } else { return((T)pool.Get(key)); } }
/// <summary> /// 添加对象 /// </summary> /// <param name="key">键值</param> /// <param name="value"></param> public virtual void Add(object key, object value) { IObjectPool pool = this.GetObjectPool(); if (pool == null) { lock (this) { pool = this.GetObjectPool(); if (pool == null) { pool = this.CreateObjectPool(); this.SetObjectPool(pool); } } } pool.Add(key, value); }