Skip to content

Commit 8b8e11b

Browse files
committed
Update V2
1 parent 9fca709 commit 8b8e11b

18 files changed

+152
-98
lines changed

Runtime/Asset/PoolAsset.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,68 @@
22

33
namespace BP.PoolIO
44
{
5+
/// <summary>
6+
/// ScriptableObject asset representing a pool for GameObjects.
7+
/// </summary>
58
[CreateAssetMenu(fileName = "Pool", menuName = "Pooling/Pool")]
69
public class PoolAsset : PoolResource
710
{
811
[SerializeField] private string poolName;
912
[SerializeField] private GameObject prefab;
10-
[SerializeField] private bool reuseObjects;
1113
[SerializeField] private int initSize;
1214
[SerializeField] private int maxSize;
15+
[SerializeField] private bool reuseObjects;
16+
[SerializeField] private bool isPersistant;
1317

18+
/// <summary>
19+
/// Gets the name of the pool.
20+
/// </summary>
1421
public string PoolName => poolName;
22+
23+
/// <summary>
24+
/// Gets the prefab associated with this pool.
25+
/// </summary>
1526
public GameObject Prefab => prefab;
16-
public bool ReuseObjects => reuseObjects;
27+
28+
/// <summary>
29+
/// Gets the initial size of the pool.
30+
/// </summary>
1731
public int InitSize => initSize;
32+
/// <summary>
33+
/// Gets the maximum size of the pool.
34+
/// </summary>
1835
public int MaxSize => maxSize;
1936

20-
private IPoolable poolRef;
37+
/// <summary>
38+
/// Gets a value indicating whether objects in the pool should be reused.
39+
/// </summary>
40+
public bool ReuseObjects => reuseObjects;
2141

22-
public override void Init()
23-
{
24-
if (PoolUtils.IsNull(poolRef))
25-
{
26-
poolRef = GetPool();
27-
}
28-
}
42+
/// <summary>
43+
/// Gets a value indicating whether the pool should persist across scene loads.
44+
/// </summary>
45+
public bool IsPersistant => isPersistant;
46+
47+
private IPool poolRef;
48+
49+
/// <summary>
50+
/// Retrieves a GameObject from the pool.
51+
/// </summary>
52+
/// <returns>The pooled GameObject.</returns>
2953
public override GameObject Get() => GetPool().Get();
54+
55+
/// <summary>
56+
/// Releases a GameObject back into the pool.
57+
/// </summary>
58+
/// <param name="gameObject">The GameObject to release.</param>
59+
/// <returns>True if the object was successfully released; otherwise, false.</returns>
3060
public override bool Release(GameObject gameObject) => poolRef?.Release(gameObject) ?? false;
31-
private IPoolable GetPool()
61+
62+
/// <summary>
63+
/// Gets the pool instance, creating it if necessary.
64+
/// </summary>
65+
/// <returns>The pool instance implementing IPoolable.</returns>
66+
private IPool GetPool()
3267
{
3368
if (PoolUtils.IsNull(poolRef))
3469
{

Runtime/Asset/PoolGroupAsset.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,49 @@
22

33
namespace BP.PoolIO
44
{
5+
/// <summary>
6+
/// Enumeration for selecting the pooling strategy.
7+
/// </summary>
58
public enum PoolPickMode
69
{
710
Random,
811
Sequential,
912
Back2Back
1013
}
1114

15+
/// <summary>
16+
/// ScriptableObject asset representing a group of pools.
17+
/// </summary>
1218
[CreateAssetMenu(fileName = "PoolGroup", menuName = "Pooling/PoolGroup")]
1319
public class PoolGroupAsset : PoolResource
1420
{
1521
[SerializeField] private string groupName;
1622
[SerializeField] private PoolPickMode pickMode = PoolPickMode.Random;
1723
[SerializeField] private PoolAsset[] pools;
24+
[SerializeField] private bool isPersistent;
1825

26+
/// <summary>
27+
/// Gets the name of the pool group.
28+
/// </summary>
1929
public string GroupName => groupName;
30+
31+
/// <summary>
32+
/// Gets the picking mode for selecting pools.
33+
/// </summary>
2034
public PoolPickMode PickMode => pickMode;
35+
36+
/// <summary>
37+
/// Gets the array of pool assets in the group.
38+
/// </summary>
2139
public PoolAsset[] Pools => pools;
2240

41+
/// <summary>
42+
/// Gets a value indicating whether the pool group should persist across scene loads.
43+
/// </summary>
44+
public bool IsPersistent => isPersistent;
45+
2346
private PoolGroup groupRef;
2447

25-
public override void Init()
26-
{
27-
if (PoolUtils.IsNull(groupRef))
28-
{
29-
groupRef = GetPoolGroup();
30-
}
31-
}
3248
public override GameObject Get() => GetPoolGroup().Get();
3349
public override bool Release(GameObject gameObject)
3450
{

Runtime/Asset/PoolResource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace BP.PoolIO
44
{
5-
public abstract class PoolResource : ScriptableObject, IPoolable
5+
public abstract class PoolResource : ScriptableObject, IPool
66
{
7-
public abstract void Init();
87
public abstract GameObject Get();
98
public abstract bool Release(GameObject gameObject);
109
}

Runtime/Components/Pool.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public void SetAsset(PoolAsset descriptor)
2525
prefab = descriptor.Prefab;
2626
reuseObjects = descriptor.ReuseObjects;
2727
initSize = descriptor.InitSize;
28+
dontDestroyOnLoad = descriptor.IsPersistant;
2829
}
2930

30-
private void Start() => Init();
31-
public override void Init()
31+
private void Start()
3232
{
3333
if (dontDestroyOnLoad)
3434
{
@@ -41,28 +41,38 @@ public override void Init()
4141
availableQueue.Enqueue(item);
4242
}
4343
}
44+
4445
public override GameObject Get()
4546
{
4647
GameObject item;
4748
if (availableQueue.Count > 0)
4849
{
4950
item = availableQueue.Dequeue();
50-
usedList.AddFirst(item);
5151
}
5252
else if (reuseObjects && usedList.Count > 0)
5353
{
5454
item = usedList.Last.Value;
5555
usedList.RemoveLast();
56-
usedList.AddFirst(item);
56+
PrepareForReuse(item);
5757
}
5858
else
5959
{
6060
item = CreatePooledObject();
61-
usedList.AddFirst(item);
6261
}
6362

63+
usedList.AddFirst(item);
6464
return item;
6565
}
66+
private void PrepareForReuse(GameObject pooledObject)
67+
{
68+
if (pooledObject.TryGetComponent<IPoolable>(out var poolable))
69+
{
70+
poolable.OnReuse();
71+
}
72+
73+
pooledObject.SetActive(false);
74+
}
75+
6676
private GameObject CreatePooledObject()
6777
{
6878
var go = Instantiate(prefab);
@@ -82,5 +92,14 @@ public override bool Release(GameObject gameObject)
8292
}
8393
return false;
8494
}
95+
96+
public override void MakePersistent()
97+
{
98+
if (!dontDestroyOnLoad)
99+
{
100+
dontDestroyOnLoad = true;
101+
DontDestroyOnLoad(gameObject);
102+
}
103+
}
85104
}
86105
}

Runtime/Components/PoolComponent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace BP.PoolIO
55
/// <summary>
66
/// Base class for components that implemnt IPool interface.
77
/// </summary>
8-
public abstract class PoolComponent : MonoBehaviour, IPoolable
8+
public abstract class PoolComponent : MonoBehaviour, IPool
99
{
10-
public abstract void Init();
1110
public abstract GameObject Get();
1211
public abstract bool Release(GameObject gameObject);
12+
public abstract void MakePersistent();
1313
}
1414
}

Runtime/Components/PoolGroup.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
namespace BP.PoolIO
55
{
6+
/// <summary>
7+
/// Represents a group of pools that can use different selection strategies to obtain pooled GameObjects.
8+
/// </summary>
69
public class PoolGroup : PoolComponent
710
{
811
[SerializeField] private PoolPickMode pickMode = PoolPickMode.Random;
912
[SerializeField] private List<PoolComponent> pools = new();
13+
[SerializeField] private bool isPersistent;
1014

1115
private int seqIndex;
1216
private int backIndex;
@@ -39,6 +43,7 @@ private void OnValidate()
3943
public void SetAsset(PoolGroupAsset poolGroupAsset)
4044
{
4145
pickMode = poolGroupAsset.PickMode;
46+
isPersistent = poolGroupAsset.IsPersistent;
4247
}
4348

4449
public void AddPool(PoolComponent pool)
@@ -51,6 +56,7 @@ public void AddPool(PoolComponent pool)
5156
return;
5257
}
5358

59+
if (isPersistent) pool.MakePersistent();
5460
pools.Add(pool);
5561
}
5662
public bool RemovePool(PoolComponent pool)
@@ -72,14 +78,6 @@ public bool ContainsPool(PoolComponent targetPool)
7278
return false;
7379
}
7480

75-
public override void Init()
76-
{
77-
foreach (var pool in pools)
78-
{
79-
if (pool == null) continue;
80-
pool.Init();
81-
}
82-
}
8381
public override GameObject Get()
8482
{
8583
return pickMode switch
@@ -126,5 +124,13 @@ public override bool Release(GameObject gameObject)
126124
return false;
127125
}
128126

127+
128+
public override void MakePersistent()
129+
{
130+
foreach (var pool in pools)
131+
{
132+
pool.MakePersistent();
133+
}
134+
}
129135
}
130136
}

Runtime/Components/PoolInit.cs

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

Runtime/Components/PoolInit.cs.meta

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

Runtime/Components/PoolObject.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
using UnityEngine;
2+
using UnityEngine.Events;
23

34
namespace BP.PoolIO
45
{
5-
public enum PoolObjectMode
6+
public class PoolObject : MonoBehaviour, IPoolable
67
{
7-
Time,
8-
Disable
9-
}
10-
public class PoolObject : MonoBehaviour
11-
{
12-
[SerializeField] private PoolObjectMode mode;
13-
[SerializeField] private float time;
14-
15-
private float timer;
16-
private void Update()
8+
[SerializeField] private UnityEvent Reused;
9+
public void OnReuse()
1710
{
18-
11+
Reused?.Invoke();
1912
}
2013
}
2114
}

Runtime/Interfaces/IPool.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityEngine;
2+
3+
namespace BP.PoolIO
4+
{
5+
public interface IPool
6+
{
7+
/// <summary>
8+
/// Retrieves a GameObject from the pool group.
9+
/// </summary>
10+
/// <returns>The pooled GameObject.</returns>
11+
GameObject Get();
12+
13+
14+
/// <summary>
15+
/// Releases a GameObject back into the pool group.
16+
/// </summary>
17+
/// <param name="gameObject">The GameObject to release.</param>
18+
/// <returns>True if the object was successfully released; otherwise, false.</returns>
19+
bool Release(GameObject gameObject);
20+
}
21+
}

0 commit comments

Comments
 (0)