Skip to content

Commit 76e2e28

Browse files
committed
Update
1 parent 0ddfbd8 commit 76e2e28

9 files changed

+21
-102
lines changed

GlobalPool.cs

-25
This file was deleted.

GlobalPool.cs.meta

-11
This file was deleted.

LICENSE

-21
This file was deleted.

Pool.cs

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
using Sirenix.OdinInspector;
2-
using System;
32
using System.Collections.Generic;
43
using UnityEngine;
54

65
namespace ToolBox.Pools
76
{
8-
[Serializable]
9-
public class Pool
10-
{
11-
[SerializeField, AssetList, AssetsOnly] private Poolable _prefab = null;
7+
[CreateAssetMenu(menuName = "ToolBox/Pool"), Required, AssetSelector]
8+
public sealed class Pool : ScriptableObject
9+
{
10+
[SerializeField, AssetList, AssetsOnly, Required] private Poolable _prefab = null;
1211
[SerializeField] private int _startCount = 0;
13-
[SerializeField, SceneObjectsOnly] private Transform _holder = null;
1412

1513
private int _currentCount = 0;
1614
private Queue<Poolable> _entities = null;
1715

18-
public Pool(Poolable prefab, int startCount, Transform holder)
19-
{
20-
_prefab = prefab;
21-
_startCount = startCount;
22-
_holder = holder;
23-
}
24-
16+
[Button]
2517
public void Fill()
2618
{
2719
_entities = new Queue<Poolable>(_startCount);
2820
_currentCount = _startCount;
2921

3022
for (int i = 0; i < _startCount; i++)
3123
{
32-
Poolable entity = UnityEngine.Object.Instantiate(_prefab, _holder);
33-
AddToPool(entity);
34-
}
35-
36-
void AddToPool(Poolable newEntity)
37-
{
38-
newEntity.SetPool(this);
39-
_entities.Enqueue(newEntity);
40-
newEntity.gameObject.SetActive(false);
24+
Poolable entity = Instantiate(_prefab);
25+
entity.SetPool(this);
26+
_entities.Enqueue(entity);
27+
entity.gameObject.SetActive(false);
4128
}
4229
}
4330

@@ -101,7 +88,7 @@ public void ReturnEntity(Poolable entity)
10188
_entities.Enqueue(entity);
10289
_currentCount++;
10390

104-
entity.transform.SetParent(_holder, false);
91+
entity.transform.SetParent(null, false);
10592
entity.gameObject.SetActive(false);
10693
}
10794

@@ -111,7 +98,7 @@ private Poolable TakeEntity()
11198

11299
if (_currentCount == 0)
113100
{
114-
entity = UnityEngine.Object.Instantiate(_prefab, _holder);
101+
entity = Instantiate(_prefab);
115102
entity.SetPool(this);
116103

117104
return entity;
@@ -121,7 +108,7 @@ private Poolable TakeEntity()
121108

122109
if (entity == null)
123110
{
124-
entity = UnityEngine.Object.Instantiate(_prefab, _holder);
111+
entity = Instantiate(_prefab);
125112
entity.SetPool(this);
126113
_currentCount++;
127114
}

Pool.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Poolable.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace ToolBox.Pools
99
[DisallowMultipleComponent]
1010
public class Poolable : MonoBehaviour, IReactor
1111
{
12-
[SerializeField, TabGroup("Global Pool")] private GlobalPool _globalPool = null;
13-
1412
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackToPool = null;
1513
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackFromPool = null;
1614

@@ -27,12 +25,6 @@ public class Poolable : MonoBehaviour, IReactor
2725

2826
private void Awake()
2927
{
30-
if (_globalPool != null)
31-
{
32-
Pool = _globalPool.Pool;
33-
_isPooled = true;
34-
}
35-
3628
_onBackToPool.Setup();
3729
_onBackFromPool.Setup();
3830

@@ -55,13 +47,13 @@ public void ReturnToPool()
5547

5648
Pool.ReturnEntity(this);
5749
_isEnabled = false;
58-
59-
for (int i = 0; i < _poolables.Length; i++)
60-
_poolables[i].Reset();
6150
}
6251

6352
public void ReturnFromPool()
6453
{
54+
for (int i = 0; i < _poolables.Length; i++)
55+
_poolables[i].Reset();
56+
6557
_onBackFromPool.SendReaction();
6658
_isEnabled = true;
6759
}
@@ -86,6 +78,6 @@ private void OnPoolablesChange()
8678
}
8779

8880
private IEnumerable<IPoolable> GetPoolables() =>
89-
GetComponentsInChildren<IPoolable>();
81+
GetComponentsInChildren<IPoolable>(true);
9082
}
9183
}
File renamed without changes.

Global Pools/TestPool.asset renamed to Pools/Pool.asset

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ MonoBehaviour:
1010
m_Enabled: 1
1111
m_EditorHideFlags: 0
1212
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13-
m_Name: TestPool
13+
m_Name: BowTower
1414
m_EditorClassIdentifier:
15-
_pool:
16-
_prefab: {fileID: -5867640059013084840, guid: 58be6d132003ec14087e25cfa3ceb8fb,
17-
type: 3}
18-
_startCount: 5
19-
_holder: {fileID: 0}
15+
_prefab: {fileID: 8418102157395063907, guid: 245a074ef02e7bc49be09dd22bda9aeb, type: 3}
16+
_startCount: 25

Global Pools/TestPool.asset.meta renamed to Pools/Pool.asset.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)