概念
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">将被使用的对象事先创建好,保存在列表中,供客户端取用,</span>当客户端取得一个对象时,这个对象就已经是按照特定环境初始化好了,马上即可使用的了。当客户端使用完毕,需要将对象归还给“池”,最后在系统生命周期结束时,由“池”统一释放这些对象。
<span style="color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; line-height: 26px;">服务端负责数据的处理,和逻辑运算;客户端负责绘制服务端计算过后的图像。</span>
声明
<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px; color: rgb(0, 0, 0); line-height: 18px; font-family: 'Courier New' !important;"> ObjectPoolManager _bulletPool = GameObject.Find(<span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); line-height: 1.5 !important;">挂载的物体名称</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>).GetComponent<ObjectPoolManager>();</pre>
作用
<span style="color: rgb(51, 51, 51); font-family: arial, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">可以减少反复创建和初始化某个对象的系统开销</span>
对象池中元素的共同特性
<span style="color: rgb(102, 102, 102); font-family: 'Microsoft YaHei'; font-size: 15px; line-height: 26px;">存在固定生命周期</span>
<span style="color: rgb(102, 102, 102); font-family: 'Microsoft YaHei'; font-size: 15px; line-height: 26px;">使用比较频繁</span>
<span style="color: rgb(102, 102, 102); font-family: 'Microsoft YaHei'; font-size: 15px; line-height: 26px;">场景中大量使用</span>
对象池的通用结构
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">初始化对象</span>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">获取可用对象</span>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">归还对象</span>
<span style="color: rgb(0, 0, 0); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;">放回池中的时候,我们把元素的父节点也设置为池元素,这样做是避免当元素挂载的对象在内存中被删除的时候,元素也被删除的问题</span>
对象池的使用
<span style="color: rgb(61, 70, 77); font-family: 'Pingfang SC', STHeiti, 'Lantinghei SC', 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif; font-size: 16px; line-height: 28px; background-color: rgb(248, 248, 248);">borrowObject(借出池对象)</span>
<span style="color: rgb(61, 70, 77); font-family: 'Pingfang SC', STHeiti, 'Lantinghei SC', 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif; font-size: 16px; line-height: 28px; background-color: rgb(248, 248, 248);">returnObject(返回池对象)</span>
使用对象池的注意事项
<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 18px; line-height: 35px;">虽然对象池可以优化对象利用率,但是对象池也不能无限地存储对象,这样对于内存占用也是急剧增加,应该通过限制池子上限,并通过统计获取使用频率较低的对象并剔除,从而动态地收缩对象池。</span>
<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 18px; line-height: 35px;">对于重复使用的对象,在对象池中应该处理好对象重置(reset)的操作</span>
<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 18px; line-height: 35px;">假如在多个线程中同时访问统一对象池,要处理好线程安全的问题</span>
优化
<span style="color: rgb(51, 51, 51); font-family: arial, sans-serif; font-size: large; line-height: 23.04px;">使用二分法将池分为,非活动池和活动池</span>
<span style="color: rgb(51, 51, 51); font-family: arial, sans-serif; font-size: large; line-height: 23.04px;">给池分类,分成不同类型的池,减少无用的遍历</span>