Skip to content

Commit 31c4fca

Browse files
committed
Generic Getters
1 parent edf891a commit 31c4fca

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

Pool.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ToolBox.Pools
77
[System.Serializable]
88
public class Pool
99
{
10-
[SerializeField, AssetsOnly, TabGroup("Data")] private Poolable prefab = null;
10+
[SerializeField, AssetsOnly, TabGroup("Data"), AssetSelector] private Poolable prefab = null;
1111
[SerializeField, TabGroup("Data")] private int startCount = 0;
1212
[SerializeField, TabGroup("Data")] private bool isResizable = false;
1313
[SerializeField, SceneObjectsOnly, TabGroup("Data")] private Transform holder = null;
@@ -98,6 +98,18 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation, Transform paren
9898
return entity;
9999
}
100100

101+
public T GetEntity<T>() where T : Component =>
102+
GetEntity().Component as T;
103+
104+
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) where T : Component =>
105+
GetEntity(parent, spawnInWorldSpace).Component as T;
106+
107+
public T GetEntity<T>(Vector3 position, Quaternion rotation) where T : Component =>
108+
GetEntity(position, rotation).Component as T;
109+
110+
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
111+
GetEntity(position, rotation, parent, spawnInWorldSpace).Component as T;
112+
101113
public void ReturnEntity(Poolable entity)
102114
{
103115
if (entity.Pool != this)

Poolable.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1+
using Sirenix.OdinInspector;
2+
using Sirenix.Serialization;
3+
using ToolBox.Observer;
14
using UnityEngine;
2-
using UnityEngine.Events;
35

46
namespace ToolBox.Pools
57
{
68
[DisallowMultipleComponent]
7-
public class Poolable : MonoBehaviour
9+
public class Poolable : SerializedMonoBehaviour, IReactor
810
{
9-
public Pool Pool { get; private set; } = null;
11+
[SerializeField] private Component component = null;
12+
[OdinSerialize] private IReactor[] onBackToPool = null;
13+
[OdinSerialize] private IReactor[] onBackFromPool = null;
1014

11-
[SerializeField] private UnityEvent OnBackToPool = null;
12-
[SerializeField] private UnityEvent OnBackFromPool = null;
15+
public Pool Pool { get; private set; } = null;
16+
public Component Component => component;
1317

1418
private bool isPooled = false;
1519
private bool isEnabled = true;
16-
private bool wasSpawnedBefore = false;
1720

1821
public void ReturnToPool()
1922
{
2023
if (!isEnabled)
2124
return;
2225

23-
OnBackToPool?.Invoke();
26+
onBackToPool.Dispatch();
2427

2528
Pool.ReturnEntity(this);
2629
isEnabled = false;
2730
}
2831

2932
public void ReturnFromPool()
3033
{
31-
if (wasSpawnedBefore)
32-
OnBackFromPool?.Invoke();
33-
else
34-
wasSpawnedBefore = true;
35-
34+
onBackFromPool.Dispatch();
3635
isEnabled = true;
3736
}
3837

@@ -44,5 +43,8 @@ public void SetPool(Pool pool)
4443
isPooled = true;
4544
}
4645
}
46+
47+
public void HandleReaction() =>
48+
ReturnToPool();
4749
}
4850
}

0 commit comments

Comments
 (0)