generic pool made
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
using Darkmatter.Domain;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class LeaderBoardDataPool : ObjectPool<LeaderBoardData> { }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c15c9d815e963334384a40fbc8e84f23
|
||||
60
Assets/DarkMatter/Code/Presentation/Pool/ObjectPool.cs
Normal file
60
Assets/DarkMatter/Code/Presentation/Pool/ObjectPool.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Darkmatter.Core;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class ObjectPool<T> : MonoBehaviour,IPool<T> where T : MonoBehaviour, IPoolable
|
||||
{
|
||||
[SerializeField] T prefab;
|
||||
[SerializeField] private Transform prefabParent;
|
||||
[SerializeField] private int poolSize = 15;
|
||||
|
||||
public Queue<T> pool = new Queue<T>();
|
||||
[Inject] IObjectResolver resolver;
|
||||
|
||||
public IReadOnlyCollection<T> All => pool;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
CreateObjectPool();
|
||||
}
|
||||
|
||||
private void CreateObjectPool()
|
||||
{
|
||||
for(int i=0;i<poolSize; i++)
|
||||
{
|
||||
T obj = Instantiate(prefab, prefabParent);
|
||||
resolver.InjectGameObject(obj.gameObject);
|
||||
obj.OnSpawn();
|
||||
obj.gameObject.SetActive(false);
|
||||
pool.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public T GetFromPool()
|
||||
{
|
||||
if(pool.Count == 0)
|
||||
{
|
||||
T obj = Instantiate(prefab, prefabParent);
|
||||
resolver.InjectGameObject(obj.gameObject);
|
||||
obj.OnSpawn();
|
||||
obj.gameObject.SetActive(true);
|
||||
pool.Enqueue(obj);
|
||||
return obj;
|
||||
}
|
||||
T returningObj = pool.Dequeue();
|
||||
return returningObj;
|
||||
}
|
||||
|
||||
public void ReturnToPool(T obj)
|
||||
{
|
||||
obj.OnDespawn();
|
||||
obj.gameObject.SetActive(false);
|
||||
pool.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6751a0040c85b7441a706516d36a4762
|
||||
9
Assets/DarkMatter/Code/Presentation/Pool/PlatformPool.cs
Normal file
9
Assets/DarkMatter/Code/Presentation/Pool/PlatformPool.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Darkmatter.Presentation;
|
||||
|
||||
|
||||
public class PlatformPool : ObjectPool<Platform> { }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4977d8686efb09b40a678a0fb6037eee
|
||||
Reference in New Issue
Block a user