35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Pool;
|
|
namespace BITKit
|
|
{
|
|
[System.Serializable]
|
|
public class UnityPool<T> where T : Component
|
|
{
|
|
public UnityPool()
|
|
{
|
|
this.pool = new(Spawn, OnGet, OnReturn, OnDestroy, maxSize: 16);
|
|
}
|
|
[Header(Constant.Header.Prefabs)]
|
|
public T prefab;
|
|
[Header(Constant.Header.Gameobjects)]
|
|
public Transform root;
|
|
[Header(Constant.Header.InternalVariables)]
|
|
ObjectPool<T> pool;
|
|
public T Get(T element = null, Transform root = null)
|
|
{
|
|
if (element is not null)
|
|
prefab = element;
|
|
if (root is not null)
|
|
this.root = root;
|
|
return pool.Get();
|
|
}
|
|
public void Return(T element) => pool.Release(element);
|
|
T Spawn() => Transform.Instantiate(prefab, root);
|
|
void OnGet(T element) => element.gameObject.SetActive(true);
|
|
void OnReturn(T element) => element.gameObject.SetActive(false);
|
|
void OnDestroy(T element) => GameObject.Destroy(element.gameObject);
|
|
}
|
|
} |