实例池
介绍
- 介绍
- 功能实现
- 食用方法
有时候我们会在场景中实例化许多的物体,比如子弹
,实例化一个物体需要花费一定的时间,虽然它肉眼不可见,但如果在某个时刻要实例化一堆子弹呢?那它就会造成卡顿的情况发生。要解决这个问题,我们可以在加载的时候预先实例化好一小堆的子弹,就好比士兵身上的备用弹夹。这样在射击的时候就不用花时间装子弹了。而且在虚拟世界我们还可以回收那些用过的子弹循环利用,这样本来需要在场景中实例化10000的子弹可能只需要100颗就够用了。这就是今天的主角实例池
。
#mermaid-svg-zmkZV5zlDbIhA5wa .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .label text{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .node rect,#mermaid-svg-zmkZV5zlDbIhA5wa .node circle,#mermaid-svg-zmkZV5zlDbIhA5wa .node ellipse,#mermaid-svg-zmkZV5zlDbIhA5wa .node polygon,#mermaid-svg-zmkZV5zlDbIhA5wa .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zmkZV5zlDbIhA5wa .node .label{text-align:center;fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .node.clickable{cursor:pointer}#mermaid-svg-zmkZV5zlDbIhA5wa .arrowheadPath{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-zmkZV5zlDbIhA5wa .flowchart-link{stroke:#333;fill:none}#mermaid-svg-zmkZV5zlDbIhA5wa .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-zmkZV5zlDbIhA5wa .edgeLabel rect{opacity:0.9}#mermaid-svg-zmkZV5zlDbIhA5wa .edgeLabel span{color:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-zmkZV5zlDbIhA5wa .cluster text{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-zmkZV5zlDbIhA5wa .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zmkZV5zlDbIhA5wa text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-zmkZV5zlDbIhA5wa .actor-line{stroke:grey}#mermaid-svg-zmkZV5zlDbIhA5wa .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-zmkZV5zlDbIhA5wa #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .sequenceNumber{fill:#fff}#mermaid-svg-zmkZV5zlDbIhA5wa #sequencenumber{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa #crosshead path{fill:#333;stroke:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .messageText{fill:#333;stroke:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zmkZV5zlDbIhA5wa .labelText,#mermaid-svg-zmkZV5zlDbIhA5wa .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-zmkZV5zlDbIhA5wa .loopText,#mermaid-svg-zmkZV5zlDbIhA5wa .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-zmkZV5zlDbIhA5wa .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-zmkZV5zlDbIhA5wa .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zmkZV5zlDbIhA5wa .noteText,#mermaid-svg-zmkZV5zlDbIhA5wa .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-zmkZV5zlDbIhA5wa .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-zmkZV5zlDbIhA5wa .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-zmkZV5zlDbIhA5wa .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-zmkZV5zlDbIhA5wa .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .section{stroke:none;opacity:0.2}#mermaid-svg-zmkZV5zlDbIhA5wa .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-zmkZV5zlDbIhA5wa .section2{fill:#fff400}#mermaid-svg-zmkZV5zlDbIhA5wa .section1,#mermaid-svg-zmkZV5zlDbIhA5wa .section3{fill:#fff;opacity:0.2}#mermaid-svg-zmkZV5zlDbIhA5wa .sectionTitle0{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .sectionTitle1{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .sectionTitle2{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .sectionTitle3{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-zmkZV5zlDbIhA5wa .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .grid path{stroke-width:0}#mermaid-svg-zmkZV5zlDbIhA5wa .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-zmkZV5zlDbIhA5wa .task{stroke-width:2}#mermaid-svg-zmkZV5zlDbIhA5wa .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .taskText:not([font-size]){font-size:11px}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-zmkZV5zlDbIhA5wa .task.clickable{cursor:pointer}#mermaid-svg-zmkZV5zlDbIhA5wa .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zmkZV5zlDbIhA5wa .taskText0,#mermaid-svg-zmkZV5zlDbIhA5wa .taskText1,#mermaid-svg-zmkZV5zlDbIhA5wa .taskText2,#mermaid-svg-zmkZV5zlDbIhA5wa .taskText3{fill:#fff}#mermaid-svg-zmkZV5zlDbIhA5wa .task0,#mermaid-svg-zmkZV5zlDbIhA5wa .task1,#mermaid-svg-zmkZV5zlDbIhA5wa .task2,#mermaid-svg-zmkZV5zlDbIhA5wa .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutside0,#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutside2{fill:#000}#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutside1,#mermaid-svg-zmkZV5zlDbIhA5wa .taskTextOutside3{fill:#000}#mermaid-svg-zmkZV5zlDbIhA5wa .active0,#mermaid-svg-zmkZV5zlDbIhA5wa .active1,#mermaid-svg-zmkZV5zlDbIhA5wa .active2,#mermaid-svg-zmkZV5zlDbIhA5wa .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-zmkZV5zlDbIhA5wa .activeText0,#mermaid-svg-zmkZV5zlDbIhA5wa .activeText1,#mermaid-svg-zmkZV5zlDbIhA5wa .activeText2,#mermaid-svg-zmkZV5zlDbIhA5wa .activeText3{fill:#000 !important}#mermaid-svg-zmkZV5zlDbIhA5wa .done0,#mermaid-svg-zmkZV5zlDbIhA5wa .done1,#mermaid-svg-zmkZV5zlDbIhA5wa .done2,#mermaid-svg-zmkZV5zlDbIhA5wa .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-zmkZV5zlDbIhA5wa .doneText0,#mermaid-svg-zmkZV5zlDbIhA5wa .doneText1,#mermaid-svg-zmkZV5zlDbIhA5wa .doneText2,#mermaid-svg-zmkZV5zlDbIhA5wa .doneText3{fill:#000 !important}#mermaid-svg-zmkZV5zlDbIhA5wa .crit0,#mermaid-svg-zmkZV5zlDbIhA5wa .crit1,#mermaid-svg-zmkZV5zlDbIhA5wa .crit2,#mermaid-svg-zmkZV5zlDbIhA5wa .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-zmkZV5zlDbIhA5wa .activeCrit0,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCrit1,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCrit2,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-zmkZV5zlDbIhA5wa .doneCrit0,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCrit1,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCrit2,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-zmkZV5zlDbIhA5wa .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-zmkZV5zlDbIhA5wa .milestoneText{font-style:italic}#mermaid-svg-zmkZV5zlDbIhA5wa .doneCritText0,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCritText1,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCritText2,#mermaid-svg-zmkZV5zlDbIhA5wa .doneCritText3{fill:#000 !important}#mermaid-svg-zmkZV5zlDbIhA5wa .activeCritText0,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCritText1,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCritText2,#mermaid-svg-zmkZV5zlDbIhA5wa .activeCritText3{fill:#000 !important}#mermaid-svg-zmkZV5zlDbIhA5wa .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-zmkZV5zlDbIhA5wa g.classGroup text .title{font-weight:bolder}#mermaid-svg-zmkZV5zlDbIhA5wa g.clickable{cursor:pointer}#mermaid-svg-zmkZV5zlDbIhA5wa g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zmkZV5zlDbIhA5wa g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-zmkZV5zlDbIhA5wa .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-zmkZV5zlDbIhA5wa .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zmkZV5zlDbIhA5wa .dashed-line{stroke-dasharray:3}#mermaid-svg-zmkZV5zlDbIhA5wa #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa .commit-id,#mermaid-svg-zmkZV5zlDbIhA5wa .commit-msg,#mermaid-svg-zmkZV5zlDbIhA5wa .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-zmkZV5zlDbIhA5wa g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-zmkZV5zlDbIhA5wa g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zmkZV5zlDbIhA5wa g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zmkZV5zlDbIhA5wa .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zmkZV5zlDbIhA5wa .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-zmkZV5zlDbIhA5wa .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-zmkZV5zlDbIhA5wa .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zmkZV5zlDbIhA5wa .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-zmkZV5zlDbIhA5wa .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-zmkZV5zlDbIhA5wa .edgeLabel text{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zmkZV5zlDbIhA5wa .node circle.state-start{fill:black;stroke:black}#mermaid-svg-zmkZV5zlDbIhA5wa .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-zmkZV5zlDbIhA5wa #statediagram-barbEnd{fill:#9370db}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-state .divider{stroke:#9370db}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-zmkZV5zlDbIhA5wa .note-edge{stroke-dasharray:5}#mermaid-svg-zmkZV5zlDbIhA5wa .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-zmkZV5zlDbIhA5wa .error-icon{fill:#522}#mermaid-svg-zmkZV5zlDbIhA5wa .error-text{fill:#522;stroke:#522}#mermaid-svg-zmkZV5zlDbIhA5wa .edge-thickness-normal{stroke-width:2px}#mermaid-svg-zmkZV5zlDbIhA5wa .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-zmkZV5zlDbIhA5wa .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-zmkZV5zlDbIhA5wa .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-zmkZV5zlDbIhA5wa .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-zmkZV5zlDbIhA5wa .marker{fill:#333}#mermaid-svg-zmkZV5zlDbIhA5wa .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-zmkZV5zlDbIhA5wa {
color: rgba(0, 0, 0, 0.75);
font: ;
}
功能实现
预先实例化一定数量的子弹
提供子弹
发射
发射后被回收
实例池
子弹
士兵
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.Linq;
namespace ZYF
{
/*[TypeInfoBox("ଘ(੭ˊ꒳ˋ)੭✧ 实例池\n" +
"ଘ(੭ˊ꒳ˋ)੭✧ 避免经常实例化预制体" +
"")]*/
public class ZYF_InstancePool where T : Component
{
private static Dictionary poolDic = new Dictionary();
///
/// 获取实例
/// 先给空闲实例,没有再实例化一个返回
///
///
///
///
internal static T GetInstance(T prefab, Transform parent)
{
List pool = GetPool(prefab);
T instance = pool.FirstOrDefault();
pool.Remove(instance);
if (instance == null)
{
Debug.Log($"实例池没有 {prefab.name} 的空闲实例,新实例化");
instance = GameObject.Instantiate(prefab,parent);
}
else {
Debug.Log($"使用实例池 {instance.name} 的空闲实例",instance);
}
return instance;
}
private static List GetPool(T prefab)
{
List pool;
if (poolDic.ContainsKey(prefab) == false)
{
pool = new List();
poolDic.Add(prefab, pool);
}
else
{
pool = poolDic[prefab];
}
return pool;
}
///
/// 回收实例
///
///
///
internal static void RecycleInstance(T prefab, T instance)
{
if (instance != null)
{
instance.gameObject.SetActive(false);
var pool = GetPool(prefab: prefab);
pool.Add(instance);
Debug.LogError($"回收实例{instance.name}",instance);
}
else {
Debug.LogError($"回收实例失败,{prefab.name}实例已被删除");
}
}
}
}
食用方法
1.使用GetInstance
获取实例。 2.使用RecycleInstance
回收实例。