Skip to content

Commit c9275ba

Browse files
committed
Update
1 parent 979654d commit c9275ba

File tree

2 files changed

+24
-53
lines changed

2 files changed

+24
-53
lines changed

Pool.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ namespace ToolBox.Pools
77
[System.Serializable]
88
public class Pool
99
{
10-
[SerializeField, AssetsOnly, TabGroup("Data"), ValueDropdown(nameof(GetPoolables))] private Poolable prefab = null;
11-
[SerializeField, TabGroup("Data")] private int startCount = 0;
12-
[SerializeField, TabGroup("Data")] private bool isResizable = false;
13-
[SerializeField, SceneObjectsOnly, TabGroup("Data")] private Transform holder = null;
14-
15-
[SerializeField, ReadOnly, TabGroup("Debug")] private bool isFilled = false;
16-
[SerializeField, ReadOnly, TabGroup("Debug")] private int currentCount = 0;
10+
[SerializeField, AssetsOnly, ValueDropdown(nameof(GetPoolables))] private Poolable prefab = null;
11+
[SerializeField] private int startCount = 0;
12+
[SerializeField, SceneObjectsOnly] private Transform holder = null;
13+
[SerializeField, ReadOnly] private int currentCount = 0;
1714

1815
private Queue<Poolable> entities = null;
1916

20-
public Pool(Poolable prefab, int startCount, bool isResizable, Transform holder)
17+
public Pool(Poolable prefab, int startCount, Transform holder)
2118
{
2219
this.prefab = prefab;
2320
this.startCount = startCount;
24-
this.isResizable = isResizable;
2521
this.holder = holder;
2622
}
2723

@@ -30,30 +26,28 @@ private IEnumerable<Poolable> GetPoolables() =>
3026

3127
public void Fill()
3228
{
33-
if (isFilled)
34-
return;
35-
3629
entities = new Queue<Poolable>(startCount);
3730
currentCount = startCount;
3831

39-
for (int i = 0; i < startCount; i++)
32+
Poolable original = CreateObject(prefab);
33+
34+
for (int i = 0; i < startCount - 1; i++)
35+
CreateObject(original);
36+
37+
Poolable CreateObject(Poolable prototype)
4038
{
41-
Poolable entity = Object.Instantiate(prefab, holder);
39+
Poolable entity = Object.Instantiate(prototype, holder);
4240
entity.SetPool(this);
4341
entities.Enqueue(entity);
4442
entity.gameObject.SetActive(false);
45-
}
4643

47-
isFilled = true;
44+
return entity;
45+
}
4846
}
4947

5048
public Poolable GetEntity()
5149
{
5250
Poolable entity = TakeEntity();
53-
54-
if (IsEmpty(entity))
55-
return null;
56-
5751
entity.ReturnFromPool();
5852

5953
return entity;
@@ -63,9 +57,6 @@ public Poolable GetEntity(Transform parent, bool spawnInWorldSpace)
6357
{
6458
Poolable entity = TakeEntity();
6559

66-
if (IsEmpty(entity))
67-
return null;
68-
6960
entity.transform.SetParent(parent, spawnInWorldSpace);
7061
entity.ReturnFromPool();
7162

@@ -76,9 +67,6 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation)
7667
{
7768
Poolable entity = TakeEntity();
7869

79-
if (IsEmpty(entity))
80-
return null;
81-
8270
entity.transform.SetPositionAndRotation(position, rotation);
8371
entity.ReturnFromPool();
8472

@@ -88,10 +76,6 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation)
8876
public Poolable GetEntity(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
8977
{
9078
Poolable entity = TakeEntity();
91-
92-
if (IsEmpty(entity))
93-
return null;
94-
9579
Transform entityTransform = entity.transform;
9680

9781
entityTransform.SetParent(parent, spawnInWorldSpace);
@@ -127,23 +111,13 @@ public void ReturnEntity(Poolable entity)
127111

128112
private Poolable TakeEntity()
129113
{
130-
if (!isFilled)
131-
Fill();
132-
133114
Poolable entity;
134115

135116
if (currentCount == 0)
136117
{
137-
if (isResizable)
138-
{
139-
entity = Object.Instantiate(prefab, holder);
140-
entity.SetPool(this);
141-
return entity;
142-
}
143-
else
144-
{
145-
return null;
146-
}
118+
entity = Object.Instantiate(prefab, holder);
119+
entity.SetPool(this);
120+
return entity;
147121
}
148122

149123
entity = entities.Dequeue();
@@ -160,8 +134,5 @@ private Poolable TakeEntity()
160134

161135
return entity;
162136
}
163-
164-
private bool IsEmpty(Poolable entity) =>
165-
!isResizable && entity == null;
166137
}
167138
}

Poolable.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using Sirenix.OdinInspector;
22
using Sirenix.Serialization;
3-
using ToolBox.Observer;
3+
using ToolBox.Modules;
44
using UnityEngine;
55

66
namespace ToolBox.Pools
77
{
88
[DisallowMultipleComponent]
9-
public class Poolable : SerializedMonoBehaviour, IReactor
9+
public class Poolable : SerializedMonoBehaviour, IModule
1010
{
1111
[SerializeField] private Component component = null;
12-
[OdinSerialize] private IReactor[] onBackToPool = null;
13-
[OdinSerialize] private IReactor[] onBackFromPool = null;
12+
[OdinSerialize] private ModulesContainer onBackToPool = default;
13+
[OdinSerialize] private ModulesContainer onBackFromPool = default;
1414

1515
public Pool Pool { get; private set; } = null;
1616
public Component Component => component;
@@ -23,15 +23,15 @@ public void ReturnToPool()
2323
if (!isEnabled)
2424
return;
2525

26-
onBackToPool.Dispatch();
26+
onBackToPool.Process();
2727

2828
Pool.ReturnEntity(this);
2929
isEnabled = false;
3030
}
3131

3232
public void ReturnFromPool()
3333
{
34-
onBackFromPool.Dispatch();
34+
onBackFromPool.Process();
3535
isEnabled = true;
3636
}
3737

@@ -44,7 +44,7 @@ public void SetPool(Pool pool)
4444
}
4545
}
4646

47-
public void HandleReaction() =>
47+
public void Process() =>
4848
ReturnToPool();
4949
}
5050
}

0 commit comments

Comments
 (0)