70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using TMPro;
|
|||
|
|
|
|||
|
|
//Thomas09
|
|||
|
|
|
|||
|
|
public class Pooler : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public static Pooler Instance;
|
|||
|
|
|
|||
|
|
[SerializeField] private TextMeshProUGUI floatingTextObject;
|
|||
|
|
[SerializeField] private Transform textParent;
|
|||
|
|
|
|||
|
|
public List<TextMeshProUGUI> pooledObjects = new List<TextMeshProUGUI>();
|
|||
|
|
private int amountToPool = 25;
|
|||
|
|
|
|||
|
|
private readonly WaitForSeconds three = new WaitForSeconds(3.0f);
|
|||
|
|
|
|||
|
|
private RectTransform floatingTextObjectTransform;
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
floatingTextObjectTransform = floatingTextObject.GetComponent<RectTransform>();
|
|||
|
|
|
|||
|
|
TextMeshProUGUI temp;
|
|||
|
|
for (int i = 0; i < amountToPool; i++)
|
|||
|
|
{
|
|||
|
|
temp = Instantiate(floatingTextObject, floatingTextObjectTransform.position, Quaternion.identity, textParent);
|
|||
|
|
temp.gameObject.AddComponent<TranslateFloatingText>();
|
|||
|
|
temp.gameObject.SetActive(false);
|
|||
|
|
pooledObjects.Add(temp);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private TextMeshProUGUI GetPooledObject()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < amountToPool; i++)
|
|||
|
|
{
|
|||
|
|
if(!pooledObjects[i].gameObject.activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
return pooledObjects[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SpawnFloatingText(string displayString, Color color)
|
|||
|
|
{
|
|||
|
|
TextMeshProUGUI floatingText = GetPooledObject();
|
|||
|
|
if(floatingText != null)
|
|||
|
|
{
|
|||
|
|
floatingText.gameObject.SetActive(true);
|
|||
|
|
floatingText.SetText(displayString);
|
|||
|
|
floatingText.color = color;
|
|||
|
|
StartCoroutine(Recycle(floatingText.gameObject));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private IEnumerator Recycle(GameObject floatingText)
|
|||
|
|
{
|
|||
|
|
yield return three;
|
|||
|
|
floatingText.SetActive(false);
|
|||
|
|
}
|
|||
|
|
}
|