106 lines
3.0 KiB
C#
106 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace game
|
|
{
|
|
public class FloatingTextPool : MonoBehaviour
|
|
{
|
|
public static FloatingTextPool Instance { get; private set; }
|
|
|
|
[SerializeField] private GameObject floatingScoreTextPrefab;
|
|
[SerializeField] private GameObject floatingMultipleTextPrefab;
|
|
[SerializeField] private int initialScorePoolSize = 12;
|
|
[SerializeField] private int initialMultiplePoolSize = 2;
|
|
|
|
private Queue<FloatingText> m_ScorePool = new Queue<FloatingText>();
|
|
private Queue<FloatingText> m_MultiplePool = new Queue<FloatingText>();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
InitializePool();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
m_ScorePool.Clear();
|
|
m_ScorePool = null;
|
|
|
|
m_MultiplePool.Clear();
|
|
m_MultiplePool = null;
|
|
|
|
Instance = null;
|
|
}
|
|
|
|
private void InitializePool()
|
|
{
|
|
for (int i = 0; i < initialScorePoolSize; i++)
|
|
{
|
|
CreateNewScoreTextObject();
|
|
}
|
|
|
|
for (int i = 0; i < initialMultiplePoolSize; i++)
|
|
{
|
|
CreateNewMultipleTextObject();
|
|
}
|
|
}
|
|
|
|
private void CreateNewScoreTextObject()
|
|
{
|
|
var obj = Instantiate(floatingScoreTextPrefab, transform);
|
|
var floatingText = obj.GetComponent<FloatingText>();
|
|
floatingText.gameObject.SetActive(false);
|
|
m_ScorePool.Enqueue(floatingText);
|
|
}
|
|
|
|
private void CreateNewMultipleTextObject()
|
|
{
|
|
var obj = Instantiate(floatingMultipleTextPrefab, transform);
|
|
var floatingText = obj.GetComponent<FloatingText>();
|
|
floatingText.gameObject.SetActive(false);
|
|
m_MultiplePool.Enqueue(floatingText);
|
|
}
|
|
|
|
public FloatingText GetFloatingScoreText()
|
|
{
|
|
if (m_ScorePool.Count == 0)
|
|
{
|
|
CreateNewScoreTextObject();
|
|
}
|
|
|
|
var text = m_ScorePool.Dequeue();
|
|
text.gameObject.SetActive(true);
|
|
return text;
|
|
}
|
|
|
|
public FloatingText GetFloatingMultipleText()
|
|
{
|
|
if (m_ScorePool.Count == 0)
|
|
{
|
|
CreateNewMultipleTextObject();
|
|
}
|
|
|
|
var text = m_MultiplePool.Dequeue();
|
|
text.gameObject.SetActive(true);
|
|
return text;
|
|
}
|
|
|
|
public void ReturnToScorePool(FloatingText text)
|
|
{
|
|
text.gameObject.SetActive(false);
|
|
m_ScorePool.Enqueue(text);
|
|
}
|
|
public void ReturnToMultiplePool(FloatingText text)
|
|
{
|
|
text.gameObject.SetActive(false);
|
|
m_MultiplePool.Enqueue(text);
|
|
}
|
|
}
|
|
} |