55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class FBPool : MonoBehaviour
|
|
{
|
|
public static FBPool Instance { get; private set; }
|
|
public GameObject prefab;
|
|
List<GameObject> _pool = new List<GameObject>();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null) {
|
|
DestroyImmediate(gameObject);
|
|
} else {
|
|
Instance = this;
|
|
}
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
var go = Instantiate(prefab);
|
|
go.SetActive(false);
|
|
go.transform.SetParent(transform, false);
|
|
_pool.Add(go);
|
|
}
|
|
}
|
|
|
|
public GameObject GetObject()
|
|
{
|
|
if (_pool.Count == 0)
|
|
{
|
|
var go = Instantiate(prefab);
|
|
go.SetActive(false);
|
|
go.transform.SetParent(transform, false);
|
|
_pool.Add(go);
|
|
}
|
|
var obj = _pool[_pool.Count - 1];
|
|
_pool.RemoveAt(_pool.Count - 1);
|
|
obj.SetActive(true);
|
|
return obj;
|
|
}
|
|
|
|
public void ReturnObject(GameObject go)
|
|
{
|
|
go.transform.SetParent(transform, false);
|
|
go.SetActive(false);
|
|
_pool.Add(go);
|
|
}
|
|
}
|