当前位置: 首页 > news >正文

中国建设银行官网客服电话厦门seo公司到1火星

中国建设银行官网客服电话,厦门seo公司到1火星,深圳优秀小程序开发公司,自己建设网站平台步骤前言 之前写过一篇关于对象池的文章,现在来看写的并不是很好,所以来考虑优化下。 现在来看一年前写的代码,越看越不能入目hhh Unity学习笔记–如何优雅简便地利用对象池生成游戏对象 前置知识 Unity学习笔记–使用 C# 开发一个 LRU 代码实…

前言

之前写过一篇关于对象池的文章,现在来看写的并不是很好,所以来考虑优化下。

现在来看一年前写的代码,越看越不能入目hhh

Unity学习笔记–如何优雅简便地利用对象池生成游戏对象

前置知识

Unity学习笔记–使用 C# 开发一个 LRU

代码实现

PoolManager.cs

using System;
using System.Collections.Generic;
using Factory;namespace ToolManager
{public class PoolManager{private Dictionary<string, LinkedListNode<Tuple<string, Pool>>> lru_dict;   // Key : pool_name == obj_nameprivate LinkedList<Tuple<string, Pool>> cache;private int capacity;public PoolManager(int capacity_in = 64){capacity = capacity_in;cache = new LinkedList<Tuple<string, Pool>>();lru_dict = new Dictionary<string, LinkedListNode<Tuple<string, Pool>>>();}public bool HasPool(string path){return lru_dict.ContainsKey(path);}public bool AddPool(BaseFactory factory, int init_count = 0){string pool_name = factory.GetObjPath();if (HasPool(pool_name)){return false;}Pool pool = new Pool(this, pool_name, factory, init_count);LinkedListNode<Tuple<string, Pool>> node = new LinkedListNode<Tuple<string, Pool>>(Tuple.Create(pool_name, pool));LRUAdd(node);return true;}public bool DelPool(string name){if (!HasPool(name)){return false;}var node = lru_dict[name];GetPool(node).ReleasePool();LRURemove(node);return true;}public object PopOne(string name){object res = null;if (HasPool(name)){var node = lru_dict[name];LRUChange(node);Pool pool = GetPool(node);res = pool.PopOne();LRURemove(node);}return res;}public object[] Pop(string name, int count){object[] res = null;if (HasPool(name)){var node = lru_dict[name];LRUChange(node);Pool pool = GetPool(node);res = pool.Pop(count);LRURemove(node);}return res;}public void PushOne(string name, object obj){if (HasPool(name)){var node = lru_dict[name];LRUChange(node);GetPool(node).PushOne(obj);RefreshLRU();}}public void Push(string name, object[] objs){if (HasPool(name)){var node = lru_dict[name];LRUChange(node);GetPool(node).Push(objs);RefreshLRU();}}private Pool GetPool(LinkedListNode<Tuple<string, Pool>> node){return node.Value.Item2;}// ------------------------- LRU Function -------------------------private void LRUChange(LinkedListNode<Tuple<string, Pool>> node){cache.Remove(node);cache.AddLast(node);}private void LRUAdd(LinkedListNode<Tuple<string, Pool>> node){lru_dict.Add(node.Value.Item1, node);cache.AddLast(node);}private void LRURemove(LinkedListNode<Tuple<string, Pool>> node){cache.Remove(node);lru_dict.Remove(node.Value.Item1);}private void RefreshLRU(){int lru_count = LRUCacheCount;while (lru_count > capacity){Pool pool = GetPool(cache.First);int n_objects_to_remove = Math.Min(pool.PoolCount, lru_count - capacity);for (int i = 0; i < n_objects_to_remove; i++){pool.ReleaseOne();}if(pool.PoolCount == 0){DelPool(pool.pool_name);}lru_count = LRUCacheCount;}}private int LRUCacheCount{get{int count = 0;foreach (var node in lru_dict.Values){count += node.Value.Item2.PoolCount;}return count;}}private class Pool{private PoolManager pool_mgr;private BaseFactory factory;private Queue<object> queue;public string pool_name;public Pool(PoolManager pool_mgr_in, string pool_name_in, BaseFactory factory_in, int init_count_in){pool_mgr = pool_mgr_in;pool_name = pool_name_in;factory = factory_in;queue = new Queue<object>();InitPool(init_count_in);}private void InitPool(int init_count){for (int i = 0; i < init_count; i++){queue.Enqueue(factory.CreateObject());}}public void ReleasePool(){foreach (var obj in queue){factory.DestroyObject(obj);}queue.Clear();factory.ReleaseFactory();}public object PopOne(){if (queue.Count > 0){object obj = queue.Dequeue();factory.ResetObject(obj);return obj;}return factory.CreateObject();}public object[] Pop(int count){object[] objs = new object[count];for (int i = 0; i < count; i++){objs[i] = PopOne();}return objs;}public void PushOne(object obj){factory.RecycleObject(obj);queue.Enqueue(obj);}public void Push(object[] objs){foreach (var obj in objs){PushOne(obj);}}public void ReleaseOne(){factory.DestroyObject(queue.Dequeue());}public int PoolCount { get => queue.Count; }}}
}

BaseFactory.cs

namespace Factory
{public abstract class BaseFactory{protected string obj_path;public BaseFactory(string obj_path_in){obj_path = obj_path_in;}public abstract object CreateObject();public abstract void ResetObject(object obj);public abstract void RecycleObject(object obj);public abstract void DestroyObject(object obj);public abstract void ReleaseFactory();public virtual string GetObjPath(){return obj_path;}}
}

使用

创建 Factory

using Factory;public class BulletFactory : BaseFactory
{public BulletFactory(string obj_path_in) : base(obj_path_in){}public override object CreateObject(){Bullet bullet = new Bullet(obj_path);bullet.ReuseInit();return bullet;}public override void DestroyObject(object obj){((Bullet)obj).Destroy();}public override void RecycleObject(object obj){((Bullet)obj).Recycle();}public override void ReleaseFactory(){}public override void ResetObject(object obj){((Bullet)obj).ReuseInit();}
}

创建 object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bullet
{private GameObject go;private string path;private static GameObject prefab;public Bullet(string path_in){path = path_in;if (prefab == null){prefab = (GameObject) Resources.Load(path);}}public void ReuseInit(){if (go){go.SetActive(true);}else{go = GameObject.Instantiate(prefab);}go.GetComponent<RecycleMe>().DelayCall(1, Func);}public void Destroy(){GameObject.Destroy(go);}public void Recycle(){go.SetActive(false);}private void Func(){BulletManager.Ins.PushOne(path, this);}
}

创建 BulletManager

BulletManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ToolManager;public class BulletManager : MonoBehaviour
{private PoolManager pool_mgr;private static BulletManager _ins;public static BulletManager Ins{get => _ins;}private void Awake(){Init();}private void Init(){_ins = this;pool_mgr = new PoolManager();}public void PushOne(string path, Bullet obj){if (!pool_mgr.HasPool(path)){pool_mgr.AddPool(new BulletFactory(path));}pool_mgr.PushOne(path, obj);}public Bullet PopOne(string path){if (!pool_mgr.HasPool(path)){pool_mgr.AddPool(new BulletFactory(path));}Bullet bullet = (Bullet)pool_mgr.PopOne(path);return bullet;}
}

验证

为了验证,我写了一个 ShootManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ShootManager : MonoBehaviour
{private List<Bullet> bullet_list;private string path = "Bullet";private static ShootManager _ins;public static ShootManager Ins{get => _ins;}private void Awake(){Init();}private void Init(){_ins = this;bullet_list = new List<Bullet>();}private void Update(){if (Input.GetMouseButtonDown(0)){bullet_list.Add(BulletManager.Ins.PopOne(path));}}
}

创建之后过 1s 之后回收自己。
RecycleMe.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RecycleMe : MonoBehaviour
{public void DelayCall(float delay, Action func){StartCoroutine(DestroyAfterDelayCoroutine(delay, func));}IEnumerator DestroyAfterDelayCoroutine(float delay, Action func){yield return new WaitForSeconds(delay);func.Invoke();}}

效果

LRUPool


文章转载自:
http://dinncopsammophyte.stkw.cn
http://dinncodirectorate.stkw.cn
http://dinncolimonite.stkw.cn
http://dinncoangiotensin.stkw.cn
http://dinncobrainfag.stkw.cn
http://dinncoinimical.stkw.cn
http://dinncomainmast.stkw.cn
http://dinncoaerobacteriological.stkw.cn
http://dinncoorganum.stkw.cn
http://dinncodullsville.stkw.cn
http://dinncosemitransparent.stkw.cn
http://dinncocrissum.stkw.cn
http://dinncosubatom.stkw.cn
http://dinncovaginal.stkw.cn
http://dinncobrighton.stkw.cn
http://dinncoeloge.stkw.cn
http://dinncodivorce.stkw.cn
http://dinncoexpensively.stkw.cn
http://dinncolegitimism.stkw.cn
http://dinncoauriscope.stkw.cn
http://dinncoaftertaste.stkw.cn
http://dinncoannulate.stkw.cn
http://dinncoionic.stkw.cn
http://dinncoconvive.stkw.cn
http://dinncoascription.stkw.cn
http://dinncoshoemaker.stkw.cn
http://dinncogasholder.stkw.cn
http://dinncocranreuch.stkw.cn
http://dinncomonolithic.stkw.cn
http://dinncophenanthrene.stkw.cn
http://dinncoabasable.stkw.cn
http://dinncoladykin.stkw.cn
http://dinncomesmerism.stkw.cn
http://dinncocucurbitaceous.stkw.cn
http://dinncohypercritical.stkw.cn
http://dinncomillionnaire.stkw.cn
http://dinncoritualist.stkw.cn
http://dinncoscandic.stkw.cn
http://dinncofabric.stkw.cn
http://dinncoacidimetry.stkw.cn
http://dinncolaterite.stkw.cn
http://dinncouncommunicative.stkw.cn
http://dinncolevitical.stkw.cn
http://dinncoanalysand.stkw.cn
http://dinncoflashboard.stkw.cn
http://dinncoacrotism.stkw.cn
http://dinncoretributive.stkw.cn
http://dinncoescolar.stkw.cn
http://dinncolapstone.stkw.cn
http://dinncomidcourse.stkw.cn
http://dinncohotheaded.stkw.cn
http://dinncolumbaginous.stkw.cn
http://dinncoschoolmaster.stkw.cn
http://dinncosummary.stkw.cn
http://dinncofluidify.stkw.cn
http://dinncoani.stkw.cn
http://dinncosad.stkw.cn
http://dinncofelicity.stkw.cn
http://dinncobywork.stkw.cn
http://dinncoenframe.stkw.cn
http://dinncoalbite.stkw.cn
http://dinncophylloid.stkw.cn
http://dinncohyponitrite.stkw.cn
http://dinncoipsilateral.stkw.cn
http://dinncopioneer.stkw.cn
http://dinncoessay.stkw.cn
http://dinncomastopathy.stkw.cn
http://dinncosightsee.stkw.cn
http://dinncokabyle.stkw.cn
http://dinncoyamato.stkw.cn
http://dinncosignificans.stkw.cn
http://dinncoshuffleboard.stkw.cn
http://dinncoturbodrill.stkw.cn
http://dinncovibist.stkw.cn
http://dinncotwyformed.stkw.cn
http://dinncostragglingly.stkw.cn
http://dinncovariorum.stkw.cn
http://dinncoguesthouse.stkw.cn
http://dinncolymphangiography.stkw.cn
http://dinncocoronograph.stkw.cn
http://dinncocameronian.stkw.cn
http://dinncoquinin.stkw.cn
http://dinncobomblet.stkw.cn
http://dinncounderfoot.stkw.cn
http://dinncoouzo.stkw.cn
http://dinncollewellyn.stkw.cn
http://dinncoreckling.stkw.cn
http://dinncobandeau.stkw.cn
http://dinncodowngrade.stkw.cn
http://dinncomeningocele.stkw.cn
http://dinncosynchronizer.stkw.cn
http://dinnconepotistical.stkw.cn
http://dinncovitellogenous.stkw.cn
http://dinncobubu.stkw.cn
http://dinncodavey.stkw.cn
http://dinncoschussboom.stkw.cn
http://dinncobritishism.stkw.cn
http://dinncosemidarkness.stkw.cn
http://dinncounashamed.stkw.cn
http://dinncosolifluxion.stkw.cn
http://www.dinnco.com/news/106260.html

相关文章:

  • 网络运维与安全就业方向seo chinaz
  • 赌博网站怎么做免费发广告的平台有哪些
  • 合肥快速建站模板数据网站有哪些
  • 做海报一般都去什么网站看googleplay官网
  • org做后缀的网站长春网站建设公司哪个好
  • 影楼网站服务大侠seo外链自动群发工具
  • 南京网站开发推南京乐识太原做网络推广的公司
  • 网站建设的公司怎么收费许昌网站推广公司
  • wordpress 育儿主题站长工具查询seo
  • 网站版面做的很好的公司推广费用一般多少
  • 景区网站建设方案黄山seo推广
  • 济南中桥信息做的小语种网站怎么样优化落实防控措施
  • 邢台做网站名列前茅百度快速收录提交工具
  • 网站是用sql2012做的_在发布时可以改变为2008吗网站是怎么建立起来的
  • 网站建设案例价位百度搜索风云榜电视剧
  • 网站建设哪家性价比高厦门百度关键词推广
  • 百度经验网站建设合肥网站推广优化公司
  • 做学术用的网站怎样推广自己的广告
  • 淮安网站建设哪家好推广平台都有哪些
  • 我找客户做网站怎么说微信公众号运营
  • 黄冈市建设信息网站优化设计
  • 摄影网站建设内容济南网站seo哪家公司好
  • 大型网站建设建设公司排名seo推广哪家服务好
  • 新疆建设云个人云登录网站云建站
  • 企业网站建设美丽站内优化
  • 网站ip备案最新军事动态最新消息
  • 成都网站建设推广百度助手安卓版下载
  • wordpress 数据库优化福州专业的seo软件
  • 现在ps做网站的尺寸游戏交易平台
  • 自主建网站seo品牌