Skip to content

Commit 0feebb3

Browse files
committed
Update
1 parent c9275ba commit 0feebb3

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Pool.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
using Sirenix.OdinInspector;
22
using System.Collections.Generic;
3+
using ToolBox.Modules;
34
using UnityEngine;
45

56
namespace ToolBox.Pools
67
{
7-
[System.Serializable]
88
public class Pool
99
{
1010
[SerializeField, AssetsOnly, ValueDropdown(nameof(GetPoolables))] private Poolable prefab = null;
1111
[SerializeField] private int startCount = 0;
1212
[SerializeField, SceneObjectsOnly] private Transform holder = null;
13-
[SerializeField, ReadOnly] private int currentCount = 0;
13+
[SerializeField] private ModulesContainer<GameObject> objectInitializator = null;
1414

15+
private int currentCount = 0;
1516
private Queue<Poolable> entities = null;
1617

17-
public Pool(Poolable prefab, int startCount, Transform holder)
18+
public Pool(Poolable prefab, int startCount, Transform holder, ModulesContainer<GameObject> objectInitializator)
1819
{
1920
this.prefab = prefab;
2021
this.startCount = startCount;
2122
this.holder = holder;
23+
this.objectInitializator = objectInitializator;
2224
}
2325

2426
private IEnumerable<Poolable> GetPoolables() =>
@@ -29,19 +31,22 @@ public void Fill()
2931
entities = new Queue<Poolable>(startCount);
3032
currentCount = startCount;
3133

32-
Poolable original = CreateObject(prefab);
34+
Poolable original = Object.Instantiate(prefab, holder);
35+
objectInitializator.Process(original.gameObject);
3336

34-
for (int i = 0; i < startCount - 1; i++)
35-
CreateObject(original);
37+
AddToPool(original);
3638

37-
Poolable CreateObject(Poolable prototype)
39+
for (int i = 0; i < startCount - 1; i++)
3840
{
39-
Poolable entity = Object.Instantiate(prototype, holder);
40-
entity.SetPool(this);
41-
entities.Enqueue(entity);
42-
entity.gameObject.SetActive(false);
41+
Poolable entity = Object.Instantiate(original, holder);
42+
AddToPool(entity);
43+
}
4344

44-
return entity;
45+
void AddToPool(Poolable newEntity)
46+
{
47+
newEntity.SetPool(this);
48+
entities.Enqueue(newEntity);
49+
newEntity.gameObject.SetActive(false);
4550
}
4651
}
4752

@@ -117,6 +122,7 @@ private Poolable TakeEntity()
117122
{
118123
entity = Object.Instantiate(prefab, holder);
119124
entity.SetPool(this);
125+
120126
return entity;
121127
}
122128

Poolable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace ToolBox.Pools
99
public class Poolable : SerializedMonoBehaviour, IModule
1010
{
1111
[SerializeField] private Component component = null;
12-
[OdinSerialize] private ModulesContainer onBackToPool = default;
13-
[OdinSerialize] private ModulesContainer onBackFromPool = default;
12+
[SerializeField] private ModulesContainer onBackToPool = default;
13+
[SerializeField] private ModulesContainer onBackFromPool = default;
1414

1515
public Pool Pool { get; private set; } = null;
1616
public Component Component => component;

0 commit comments

Comments
 (0)