Skip to content

Commit 0ddfbd8

Browse files
committed
Update
1 parent 3b37d78 commit 0ddfbd8

File tree

9 files changed

+144
-22
lines changed

9 files changed

+144
-22
lines changed

Global Pools.meta

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

Global Pools/TestPool.asset

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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: TestPool
14+
m_EditorClassIdentifier:
15+
_pool:
16+
_prefab: {fileID: -5867640059013084840, guid: 58be6d132003ec14087e25cfa3ceb8fb,
17+
type: 3}
18+
_startCount: 5
19+
_holder: {fileID: 0}

Global Pools/TestPool.asset.meta

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

GlobalPool.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Sirenix.OdinInspector;
2+
using System;
3+
using ToolBox.Reactors;
4+
using UnityEngine;
5+
6+
namespace ToolBox.Pools
7+
{
8+
[CreateAssetMenu(menuName = "ToolBox/Global Pool"), Required, AssetSelector]
9+
public sealed class GlobalPool : ScriptableObject, ISetupable
10+
{
11+
[SerializeField, HideLabel] private Pool _pool = null;
12+
[ShowInInspector, ReadOnly, NonSerialized] private bool _isFilled = false;
13+
14+
public Pool Pool => _pool;
15+
16+
public void Setup()
17+
{
18+
if (!_isFilled)
19+
{
20+
_pool.Fill();
21+
_isFilled = true;
22+
}
23+
}
24+
}
25+
}

GlobalPool.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.

IPoolable.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ToolBox.Pools
2+
{
3+
public interface IPoolable
4+
{
5+
void Reset();
6+
}
7+
}

IPoolable.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.

Pool.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Sirenix.OdinInspector;
2+
using System;
23
using System.Collections.Generic;
3-
using ToolBox.Reactors;
44
using UnityEngine;
55

66
namespace ToolBox.Pools
77
{
8-
[System.Serializable]
8+
[Serializable]
99
public class Pool
1010
{
1111
[SerializeField, AssetList, AssetsOnly] private Poolable _prefab = null;
@@ -27,12 +27,9 @@ public void Fill()
2727
_entities = new Queue<Poolable>(_startCount);
2828
_currentCount = _startCount;
2929

30-
Poolable original = Object.Instantiate(_prefab, _holder);
31-
AddToPool(original);
32-
33-
for (int i = 0; i < _startCount - 1; i++)
30+
for (int i = 0; i < _startCount; i++)
3431
{
35-
Poolable entity = Object.Instantiate(original, _holder);
32+
Poolable entity = UnityEngine.Object.Instantiate(_prefab, _holder);
3633
AddToPool(entity);
3734
}
3835

@@ -84,17 +81,17 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation, Transform paren
8481
return entity;
8582
}
8683

87-
public T GetEntity<T>() where T : Component =>
88-
GetEntity().Component as T;
84+
public T GetEntity<T>() =>
85+
GetEntity().GetComponent<T>();
8986

90-
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) where T : Component =>
91-
GetEntity(parent, spawnInWorldSpace).Component as T;
87+
public T GetEntity<T>(Transform parent, bool spawnInWorldSpace) =>
88+
GetEntity(parent, spawnInWorldSpace).GetComponent<T>();
9289

93-
public T GetEntity<T>(Vector3 position, Quaternion rotation) where T : Component =>
94-
GetEntity(position, rotation).Component as T;
90+
public T GetEntity<T>(Vector3 position, Quaternion rotation) =>
91+
GetEntity(position, rotation).GetComponent<T>();
9592

96-
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
97-
GetEntity(position, rotation, parent, spawnInWorldSpace).Component as T;
93+
public T GetEntity<T>(Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) =>
94+
GetEntity(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
9895

9996
public void ReturnEntity(Poolable entity)
10097
{
@@ -114,7 +111,7 @@ private Poolable TakeEntity()
114111

115112
if (_currentCount == 0)
116113
{
117-
entity = Object.Instantiate(_prefab, _holder);
114+
entity = UnityEngine.Object.Instantiate(_prefab, _holder);
118115
entity.SetPool(this);
119116

120117
return entity;
@@ -124,7 +121,7 @@ private Poolable TakeEntity()
124121

125122
if (entity == null)
126123
{
127-
entity = Object.Instantiate(_prefab, _holder);
124+
entity = UnityEngine.Object.Instantiate(_prefab, _holder);
128125
entity.SetPool(this);
129126
_currentCount++;
130127
}

Poolable.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
1-
using ToolBox.Reactors;
1+
using Sirenix.OdinInspector;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using ToolBox.Reactors;
25
using UnityEngine;
36

47
namespace ToolBox.Pools
58
{
69
[DisallowMultipleComponent]
710
public class Poolable : MonoBehaviour, IReactor
811
{
9-
[SerializeField] private Component _component = null;
10-
[SerializeField] private Reactor _onBackToPool = null;
11-
[SerializeField] private Reactor _onBackFromPool = null;
12+
[SerializeField, TabGroup("Global Pool")] private GlobalPool _globalPool = null;
13+
14+
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackToPool = null;
15+
[SerializeField, TabGroup("Callbacks")] private Reactor _onBackFromPool = null;
16+
17+
[SerializeField, TabGroup("Poolables"), OnValueChanged(nameof(OnPoolablesChange)), ValueDropdown(nameof(GetPoolables)), HideInPlayMode]
18+
private MonoBehaviour[] _possiblePoolables = null;
19+
20+
[ShowInInspector, HideInEditorMode, TabGroup("Poolables"), ReadOnly]
21+
private IPoolable[] _poolables = null;
1222

1323
public Pool Pool { get; private set; } = null;
14-
public Component Component => _component;
1524

1625
private bool _isPooled = false;
1726
private bool _isEnabled = true;
1827

1928
private void Awake()
2029
{
30+
if (_globalPool != null)
31+
{
32+
Pool = _globalPool.Pool;
33+
_isPooled = true;
34+
}
35+
2136
_onBackToPool.Setup();
2237
_onBackFromPool.Setup();
38+
39+
int count = _possiblePoolables.Length;
40+
_poolables = new IPoolable[count];
41+
42+
for (int i = 0; i < count; i++)
43+
_poolables[i] = _possiblePoolables[i] as IPoolable;
44+
45+
_possiblePoolables = null;
2346
}
2447

2548
[Button]
@@ -32,6 +55,9 @@ public void ReturnToPool()
3255

3356
Pool.ReturnEntity(this);
3457
_isEnabled = false;
58+
59+
for (int i = 0; i < _poolables.Length; i++)
60+
_poolables[i].Reset();
3561
}
3662

3763
public void ReturnFromPool()
@@ -51,5 +77,15 @@ public void SetPool(Pool pool)
5177

5278
public void HandleReaction() =>
5379
ReturnToPool();
80+
81+
private void OnPoolablesChange()
82+
{
83+
IEnumerable<MonoBehaviour> poolables = _possiblePoolables.Where(x => x is IPoolable);
84+
poolables = poolables.GroupBy(x => x.GetHashCode()).Select(y => y.First());
85+
_possiblePoolables = poolables.ToArray();
86+
}
87+
88+
private IEnumerable<IPoolable> GetPoolables() =>
89+
GetComponentsInChildren<IPoolable>();
5490
}
5591
}

0 commit comments

Comments
 (0)