summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/ObjectPoolBehavior.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-12-30 20:59:04 +0800
committerchai <chaifix@163.com>2020-12-30 20:59:04 +0800
commite9ea621b93fbb58d9edfca8375918791637bbd52 (patch)
tree19ce3b1c1f2d51eda6878c9d0f2c9edc27f13650 /Client/Assembly-CSharp/ObjectPoolBehavior.cs
+init
Diffstat (limited to 'Client/Assembly-CSharp/ObjectPoolBehavior.cs')
-rw-r--r--Client/Assembly-CSharp/ObjectPoolBehavior.cs138
1 files changed, 138 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/ObjectPoolBehavior.cs b/Client/Assembly-CSharp/ObjectPoolBehavior.cs
new file mode 100644
index 0000000..657837b
--- /dev/null
+++ b/Client/Assembly-CSharp/ObjectPoolBehavior.cs
@@ -0,0 +1,138 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class ObjectPoolBehavior : IObjectPool
+{
+ public override int InUse
+ {
+ get
+ {
+ return this.activeChildren.Count;
+ }
+ }
+
+ public override int NotInUse
+ {
+ get
+ {
+ return this.inactiveChildren.Count;
+ }
+ }
+
+ public int poolSize = 20;
+
+ [SerializeField]
+ private List<PoolableBehavior> inactiveChildren = new List<PoolableBehavior>();
+
+ [SerializeField]
+ public List<PoolableBehavior> activeChildren = new List<PoolableBehavior>();
+
+ public PoolableBehavior Prefab;
+
+ public bool AutoInit;
+
+ public bool DetachOnGet;
+
+ public virtual void Awake()
+ {
+ if (this.AutoInit)
+ {
+ this.InitPool(this.Prefab);
+ }
+ }
+
+ public void InitPool(PoolableBehavior prefab)
+ {
+ this.AutoInit = false;
+ for (int i = 0; i < this.poolSize; i++)
+ {
+ this.CreateOneInactive(prefab);
+ }
+ }
+
+ private void CreateOneInactive(PoolableBehavior prefab)
+ {
+ PoolableBehavior poolableBehavior = UnityEngine.Object.Instantiate<PoolableBehavior>(prefab);
+ poolableBehavior.transform.SetParent(base.transform);
+ poolableBehavior.gameObject.SetActive(false);
+ poolableBehavior.OwnerPool = this;
+ this.inactiveChildren.Add(poolableBehavior);
+ }
+
+ public void ReclaimOldest()
+ {
+ if (this.activeChildren.Count > 0)
+ {
+ this.Reclaim(this.activeChildren[0]);
+ return;
+ }
+ this.InitPool(this.Prefab);
+ }
+
+ public void ReclaimAll()
+ {
+ foreach (PoolableBehavior obj in this.activeChildren.ToArray())
+ {
+ this.Reclaim(obj);
+ }
+ }
+
+ public override T Get<T>()
+ {
+ List<PoolableBehavior> obj = this.inactiveChildren;
+ PoolableBehavior poolableBehavior;
+ lock (obj)
+ {
+ if (this.inactiveChildren.Count == 0)
+ {
+ if (this.activeChildren.Count == 0)
+ {
+ this.InitPool(this.Prefab);
+ }
+ else
+ {
+ this.CreateOneInactive(this.Prefab);
+ }
+ }
+ poolableBehavior = this.inactiveChildren[this.inactiveChildren.Count - 1];
+ this.inactiveChildren.RemoveAt(this.inactiveChildren.Count - 1);
+ this.activeChildren.Add(poolableBehavior);
+ }
+ if (this.DetachOnGet)
+ {
+ poolableBehavior.transform.SetParent(null, false);
+ }
+ poolableBehavior.gameObject.SetActive(true);
+ poolableBehavior.Reset();
+ return poolableBehavior as T;
+ }
+
+ public override void Reclaim(PoolableBehavior obj)
+ {
+ if (!this)
+ {
+ DefaultPool.Instance.Reclaim(obj);
+ return;
+ }
+ obj.gameObject.SetActive(false);
+ obj.transform.SetParent(base.transform);
+ List<PoolableBehavior> obj2 = this.inactiveChildren;
+ lock (obj2)
+ {
+ if (this.activeChildren.Remove(obj))
+ {
+ this.inactiveChildren.Add(obj);
+ }
+ else if (this.inactiveChildren.Contains(obj))
+ {
+ Debug.Log("ObjectPoolBehavior: :| Something was reclaimed without being gotten");
+ }
+ else
+ {
+ Debug.Log("ObjectPoolBehavior: Destroying this thing I don't own");
+ UnityEngine.Object.Destroy(obj.gameObject);
+ }
+ }
+ }
+}