Skip to content

Commit 9874d13

Browse files
committed
Rework
1 parent 3dad9bb commit 9874d13

File tree

10 files changed

+189
-91
lines changed

10 files changed

+189
-91
lines changed

Pool.cs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
1-
#if ODIN_INSPECTOR
2-
using Sirenix.OdinInspector;
3-
#endif
4-
using System.Collections.Generic;
1+
using System.Collections.Generic;
52
using UnityEngine;
63

74
namespace ToolBox.Pools
85
{
9-
#if ODIN_INSPECTOR
10-
[Required, AssetSelector]
11-
#endif
12-
[CreateAssetMenu(menuName = "ToolBox/Pool")]
13-
public sealed class Pool : ScriptableObject
14-
{
15-
#if ODIN_INSPECTOR
16-
[Required, ValueDropdown(nameof(GetPoolables))]
17-
#endif
18-
[SerializeField] private Poolable _prefab = null;
19-
[SerializeField] private int _startCount = 0;
20-
6+
public sealed class Pool
7+
{
8+
private Poolable _prefab = null;
219
private int _currentCount = 0;
2210
private Queue<Poolable> _entities = null;
2311

24-
private void OnValidate()
12+
private static Dictionary<int, Pool> _pools = new Dictionary<int, Pool>();
13+
14+
public Pool(Poolable prefab, int count)
2515
{
26-
if (_prefab != null)
27-
_prefab.SetPool(this);
16+
_prefab = prefab;
17+
_currentCount = count;
18+
_entities = new Queue<Poolable>(count);
19+
_pools.Add(prefab.gameObject.GetHashCode(), this);
20+
21+
Populate(count);
2822
}
2923

30-
public void Fill()
24+
/// <summary>
25+
/// Prefab must contain Poolable component
26+
/// </summary>
27+
public Pool(GameObject prefab, int count)
3128
{
32-
_entities = new Queue<Poolable>(_startCount);
33-
_currentCount = _startCount;
29+
_prefab = prefab.GetComponent<Poolable>();
3430

35-
for (int i = 0; i < _startCount; i++)
31+
if (_prefab == null)
3632
{
37-
Poolable entity = Instantiate(_prefab);
33+
_prefab = Object.Instantiate(prefab).AddComponent<Poolable>();
34+
_prefab.gameObject.SetActive(false);
35+
}
36+
37+
_currentCount = count;
38+
_entities = new Queue<Poolable>(count);
39+
_pools.Add(prefab.GetHashCode(), this);
40+
41+
Populate(count);
42+
}
43+
44+
public static Pool Get(GameObject prefab)
45+
{
46+
var hasPool = _pools.TryGetValue(prefab.GetHashCode(), out var pool);
47+
48+
if (!hasPool)
49+
pool = new Pool(prefab, 0);
50+
51+
return pool;
52+
}
53+
54+
public void Populate(int count)
55+
{
56+
for (int i = 0; i < count; i++)
57+
{
58+
Poolable entity = Object.Instantiate(_prefab);
3859
entity.SetPool(this);
3960
_entities.Enqueue(entity);
4061
entity.gameObject.SetActive(false);
4162
}
63+
64+
_currentCount += count;
4265
}
4366

4467
public Poolable GetEntity()
@@ -111,7 +134,7 @@ private Poolable TakeEntity()
111134

112135
if (_currentCount == 0)
113136
{
114-
entity = Instantiate(_prefab);
137+
entity = Object.Instantiate(_prefab);
115138
entity.SetPool(this);
116139

117140
return entity;
@@ -121,7 +144,7 @@ private Poolable TakeEntity()
121144

122145
if (entity == null)
123146
{
124-
entity = Instantiate(_prefab);
147+
entity = Object.Instantiate(_prefab);
125148
entity.SetPool(this);
126149
_currentCount++;
127150
}
@@ -131,8 +154,5 @@ private Poolable TakeEntity()
131154

132155
return entity;
133156
}
134-
135-
private IEnumerable<Poolable> GetPoolables() =>
136-
Resources.FindObjectsOfTypeAll<Poolable>();
137157
}
138158
}

PoolExtensions.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using UnityEngine;
2+
3+
namespace ToolBox.Pools
4+
{
5+
public static class PoolExtensions
6+
{
7+
/// <summary>
8+
/// Use this method only with Prefabs
9+
/// </summary>
10+
public static void Populate(this GameObject prefab, int count)
11+
{
12+
var pool = Pool.Get(prefab);
13+
pool.Populate(count);
14+
}
15+
16+
/// <summary>
17+
/// Use this method only with Prefabs
18+
/// </summary>
19+
public static Poolable Spawn(this GameObject prefab)
20+
{
21+
var pool = Pool.Get(prefab);
22+
var entity = pool.GetEntity();
23+
24+
return entity;
25+
}
26+
27+
/// <summary>
28+
/// Use this method only with Prefabs
29+
/// </summary>
30+
public static Poolable Spawn(this GameObject prefab, Transform parent, bool spawnInWorldSpace)
31+
{
32+
var pool = Pool.Get(prefab);
33+
var entity = pool.GetEntity(parent, spawnInWorldSpace);
34+
35+
return entity;
36+
}
37+
38+
/// <summary>
39+
/// Use this method only with Prefabs
40+
/// </summary>
41+
public static Poolable Spawn(this GameObject prefab, Vector3 position, Quaternion rotation)
42+
{
43+
var pool = Pool.Get(prefab);
44+
var entity = pool.GetEntity(position, rotation);
45+
46+
return entity;
47+
}
48+
49+
/// <summary>
50+
/// Use this method only with Prefabs
51+
/// </summary>
52+
public static Poolable Spawn(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace)
53+
{
54+
var pool = Pool.Get(prefab);
55+
var entity = pool.GetEntity(position, rotation, parent, spawnInWorldSpace);
56+
57+
return entity;
58+
}
59+
60+
/// <summary>
61+
/// Use this method only with Prefabs
62+
/// </summary>
63+
public static T Spawn<T>(this GameObject prefab) where T : Component =>
64+
prefab.Spawn().GetComponent<T>();
65+
66+
/// <summary>
67+
/// Use this method only with Prefabs
68+
/// </summary>
69+
public static T Spawn<T>(this GameObject prefab, Transform parent, bool spawnInWorldSpace) where T : Component =>
70+
prefab.Spawn(parent, spawnInWorldSpace).GetComponent<T>();
71+
72+
/// <summary>
73+
/// Use this method only with Prefabs
74+
/// </summary>
75+
public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion rotation) where T : Component =>
76+
prefab.Spawn(position, rotation).GetComponent<T>();
77+
78+
/// <summary>
79+
/// Use this method only with Prefabs
80+
/// </summary>
81+
public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
82+
prefab.Spawn(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
83+
84+
/// <summary>
85+
/// Use this method only with Instances of Prefab
86+
/// </summary>
87+
public static void Despawn(this Poolable instance) =>
88+
instance.ReturnToPool();
89+
90+
/// <summary>
91+
/// Use this method only with Instances of Prefab
92+
/// </summary>
93+
public static void Despawn(this GameObject instance) =>
94+
instance.GetComponent<Poolable>().ReturnToPool();
95+
96+
/// <summary>
97+
/// Use this method only with Instances of Prefab
98+
/// </summary>
99+
public static void Despawn(this Transform instance) =>
100+
instance.GetComponent<Poolable>().ReturnToPool();
101+
}
102+
}

PoolExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PoolFiller.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

PoolInstaller.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
3+
namespace ToolBox.Pools
4+
{
5+
[DefaultExecutionOrder(-9999)]
6+
public class PoolInstaller : MonoBehaviour
7+
{
8+
[SerializeField] private PoolContainer[] _pools = null;
9+
10+
private void Awake()
11+
{
12+
for (int i = 0; i < _pools.Length; i++)
13+
_pools[i].Populate();
14+
}
15+
16+
[System.Serializable]
17+
private struct PoolContainer
18+
{
19+
[SerializeField] private GameObject _prefab;
20+
[SerializeField, Min(1)] private int _startCount;
21+
22+
public void Populate() =>
23+
new Pool(_prefab, _startCount);
24+
}
25+
}
26+
}
File renamed without changes.

Poolable.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@ namespace ToolBox.Pools
88
[DisallowMultipleComponent]
99
public class Poolable : MonoBehaviour
1010
{
11-
[SerializeField] private Pool _pool = null;
12-
1311
public Pool Pool { get; private set; } = null;
1412

1513
private IPoolable[] _poolables = null;
1614
private bool _isPooled = false;
1715
private bool _isEnabled = true;
1816

19-
private void Awake()
20-
{
21-
if (_pool != null)
22-
SetPool(_pool);
23-
17+
private void Awake() =>
2418
_poolables = GetComponentsInChildren<IPoolable>(true);
25-
}
2619

2720
#if ODIN_INSPECTOR
2821
[Button]

Pools.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Pools/Example.asset

Lines changed: 0 additions & 16 deletions
This file was deleted.

Pools/Example.asset.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)