备份CatanBuilding瘦身独立工程
This commit is contained in:
106
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs
Normal file
106
Assets/Scripts/EventPinball/tips/FloatingTextPool.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user