Fix Object Instantiating
This commit is contained in:
@@ -1,35 +1,49 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Enums;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
using Views;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Services {
|
||||
public class ObjectPoolService:IObjectPool<GemView> {
|
||||
private readonly GemView prefab;
|
||||
private readonly GemView[] prefabs;
|
||||
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;
|
||||
public ObjectPoolService(GemView[] prefabs, Transform parent, int size = 5) {
|
||||
this.prefabs = prefabs;
|
||||
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 GemView Get(GemType type, Vector2Int position, float dropHeight) {
|
||||
int typeAsInt = (int) type;
|
||||
|
||||
GemView gemView;
|
||||
if (this.pool.Count > 0) {
|
||||
gemView = this.pool.Pop();
|
||||
gemView.transform.localPosition = new Vector2(position.x, position.y + dropHeight);
|
||||
return gemView;
|
||||
}
|
||||
|
||||
gemView = Object.Instantiate(this.prefabs[typeAsInt], new Vector2(position.x, position.y + dropHeight), Quaternion.identity, this.parent);
|
||||
return gemView;
|
||||
}
|
||||
|
||||
public void Release(GemView gameObject) {
|
||||
this.pool.Push(gameObject);
|
||||
|
||||
public void Release(GemView gemView) {
|
||||
if (gemView == null)
|
||||
return;
|
||||
|
||||
gemView.gameObject.SetActive(false);
|
||||
this.pool.Push(gemView);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
|
||||
Reference in New Issue
Block a user