Files
match3-unity/Assets/Scripts/Services/ObjectPoolService.cs

39 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Services.Interfaces;
using UnityEngine;
using Views;
namespace Services {
public class ObjectPoolService:IObjectPool<GemView> {
private readonly GemView prefab;
private readonly Transform parent;
private readonly int size;
private readonly Stack<GemView> pool = new Stack<GemView>();
public ObjectPoolService(GemView prefab, Transform parent, int size = 5) {
this.prefab = prefab;
this.parent = parent;
this.size = size;
}
public GemView Get() {
return this.pool.Count == 0 ? Object.Instantiate(this.prefab, this.parent) : this.pool.Pop();
}
public void Fill() {
for (int i = 0; i < this.size; i++) {
Object.Instantiate(this.prefab, this.parent);
}
}
public void Release(GemView gameObject) {
this.pool.Push(gameObject);
}
public void Clear() {
this.pool.Clear();
}
}
}