Skip to content

Commit f475bcc

Browse files
committed
Update
1 parent 76e2e28 commit f475bcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+482
-47
lines changed

IPoolable.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public interface IPoolable
44
{
5-
void Reset();
5+
void OnSpawn();
6+
void OnDespawn();
67
}
78
}

Pool.cs

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
using Sirenix.OdinInspector;
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
24
using System.Collections.Generic;
35
using UnityEngine;
46

57
namespace ToolBox.Pools
68
{
7-
[CreateAssetMenu(menuName = "ToolBox/Pool"), Required, AssetSelector]
9+
#if ODIN_INSPECTOR
10+
[Required, AssetSelector]
11+
#endif
12+
[CreateAssetMenu(menuName = "ToolBox/Pool")]
813
public sealed class Pool : ScriptableObject
914
{
10-
[SerializeField, AssetList, AssetsOnly, Required] private Poolable _prefab = null;
15+
#if ODIN_INSPECTOR
16+
[Required, ValueDropdown(nameof(GetPoolables))]
17+
#endif
18+
[SerializeField] private Poolable _prefab = null;
1119
[SerializeField] private int _startCount = 0;
1220

1321
private int _currentCount = 0;
1422
private Queue<Poolable> _entities = null;
1523

16-
[Button]
24+
private void OnValidate()
25+
{
26+
if (_prefab != null)
27+
_prefab.SetPool(this);
28+
}
29+
1730
public void Fill()
1831
{
1932
_entities = new Queue<Poolable>(_startCount);
@@ -68,16 +81,16 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation, Transform paren
6881
return entity;
6982
}
7083

71-
public T GetEntity<T>() =>
84+
public T GetEntity<T>() where T : Component =>
7285
GetEntity().GetComponent<T>();
7386

74-
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) =>
87+
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) where T : Component =>
7588
GetEntity(parent, spawnInWorldSpace).GetComponent<T>();
7689

77-
public T GetEntity<T>(Vector3 position, Quaternion rotation) =>
90+
public T GetEntity<T>(Vector3 position, Quaternion rotation) where T : Component =>
7891
GetEntity(position, rotation).GetComponent<T>();
7992

80-
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) =>
93+
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
8194
GetEntity(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
8295

8396
public void ReturnEntity(Poolable entity)
@@ -118,5 +131,8 @@ private Poolable TakeEntity()
118131

119132
return entity;
120133
}
134+
135+
private IEnumerable<Poolable> GetPoolables() =>
136+
Resources.FindObjectsOfTypeAll<Poolable>();
121137
}
122138
}

PoolFiller.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
4+
using UnityEngine;
5+
6+
namespace ToolBox.Pools
7+
{
8+
[DefaultExecutionOrder(-9999)]
9+
public class PoolFiller : MonoBehaviour
10+
{
11+
#if ODIN_INSPECTOR
12+
[AssetList(AutoPopulate = true)]
13+
#endif
14+
[SerializeField] private Pool[] _pools = null;
15+
16+
private void Awake()
17+
{
18+
for (int i = 0; i < _pools.Length; i++)
19+
_pools[i].Fill();
20+
}
21+
}
22+
}

PoolFiller.cs.meta

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

Poolable.cs

+14-38
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,39 @@
1-
using Sirenix.OdinInspector;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using ToolBox.Reactors;
1+
#if ODIN_INSPECTOR
2+
using Sirenix.OdinInspector;
3+
#endif
54
using UnityEngine;
65

76
namespace ToolBox.Pools
87
{
98
[DisallowMultipleComponent]
10-
public class Poolable : MonoBehaviour, IReactor
9+
public class Poolable : MonoBehaviour
1110
{
12-
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackToPool = null;
13-
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackFromPool = null;
14-
15-
[SerializeField, TabGroup("Poolables"), OnValueChanged(nameof(OnPoolablesChange)), ValueDropdown(nameof(GetPoolables)), HideInPlayMode]
16-
private MonoBehaviour[] _possiblePoolables = null;
17-
18-
[ShowInInspector, HideInEditorMode, TabGroup("Poolables"), ReadOnly]
19-
private IPoolable[] _poolables = null;
11+
[SerializeField] private Pool _pool = null;
2012

2113
public Pool Pool { get; private set; } = null;
2214

15+
private IPoolable[] _poolables = null;
2316
private bool _isPooled = false;
2417
private bool _isEnabled = true;
2518

2619
private void Awake()
2720
{
28-
_onBackToPool.Setup();
29-
_onBackFromPool.Setup();
30-
31-
int count = _possiblePoolables.Length;
32-
_poolables = new IPoolable[count];
33-
34-
for (int i = 0; i < count; i++)
35-
_poolables[i] = _possiblePoolables[i] as IPoolable;
21+
if (_pool != null)
22+
SetPool(_pool);
3623

37-
_possiblePoolables = null;
24+
_poolables = GetComponentsInChildren<IPoolable>(true);
3825
}
3926

27+
#if ODIN_INSPECTOR
4028
[Button]
29+
#endif
4130
public void ReturnToPool()
4231
{
4332
if (!_isEnabled)
4433
return;
4534

46-
_onBackToPool.SendReaction();
35+
for (int i = 0; i < _poolables.Length; i++)
36+
_poolables[i].OnDespawn();
4737

4838
Pool.ReturnEntity(this);
4939
_isEnabled = false;
@@ -52,9 +42,8 @@ public void ReturnToPool()
5242
public void ReturnFromPool()
5343
{
5444
for (int i = 0; i < _poolables.Length; i++)
55-
_poolables[i].Reset();
45+
_poolables[i].OnSpawn();
5646

57-
_onBackFromPool.SendReaction();
5847
_isEnabled = true;
5948
}
6049

@@ -66,18 +55,5 @@ public void SetPool(Pool pool)
6655
_isPooled = true;
6756
}
6857
}
69-
70-
public void HandleReaction() =>
71-
ReturnToPool();
72-
73-
private void OnPoolablesChange()
74-
{
75-
IEnumerable<MonoBehaviour> poolables = _possiblePoolables.Where(x => x is IPoolable);
76-
poolables = poolables.GroupBy(x => x.GetHashCode()).Select(y => y.First());
77-
_possiblePoolables = poolables.ToArray();
78-
}
79-
80-
private IEnumerable<IPoolable> GetPoolables() =>
81-
GetComponentsInChildren<IPoolable>(true);
8258
}
8359
}
File renamed without changes.
File renamed without changes.

Pools/BurstVFX.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: BurstVFX
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 8343283593454105818, guid: 391a7c89b9ec5624baf6c8281c8bec2e, type: 3}
16+
_startCount: 10

Pools/BurstVFX.asset.meta

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

Pools/Crystal.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: Crystal
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 4233329813421098570, guid: e54ed93dc65d96e4bab568bd8d4c4965, type: 3}
16+
_startCount: 1

Pools/Crystal.asset.meta

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

Pools/DamagePopUp.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: DamagePopUp
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 4167792799901504813, guid: 2efe23649e0d6d44b873d0d39da59804, type: 3}
16+
_startCount: 10

Pools/DamagePopUp.asset.meta

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

Pools/FireTower.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: FireTower
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 94560056120272761, guid: 19f5e1e03705a0542b1dbf66fcaefe82, type: 3}
16+
_startCount: 25

Pools/FireTower.asset.meta

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

Pools/FireTowerProjectiles.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: FireTowerProjectiles
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 8136396646992551735, guid: ebdd9395734e5be47bee6c4d5795c843, type: 3}
16+
_startCount: 25

Pools/FireTowerProjectiles.asset.meta

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

Pools/HealVFX.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: HealVFX
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 4140721314838643587, guid: 341feda76e6d876468f3845789fac52b, type: 3}
16+
_startCount: 5

Pools/HealVFX.asset.meta

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

Pools/HitVFX.asset

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: b1b86f0f7a2abb3438596be3b0b6018e, type: 3}
13+
m_Name: HitVFX
14+
m_EditorClassIdentifier:
15+
_prefab: {fileID: 4140721314838643587, guid: e62701cc0cb7c4c4bb399c77f17df390, type: 3}
16+
_startCount: 10

Pools/HitVFX.asset.meta

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

0 commit comments

Comments
 (0)