Files
Webview_SgConfig_Unity_IOS/Assets/Scripts/ToastPool.cs
T
2026-07-15 16:19:07 +08:00

40 lines
936 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToastPool : MonoBehaviour
{
public static ToastPool Instance;
[SerializeField] private Toast toastPrefab; // Toast预制体
[SerializeField] private Transform poolParent; // 对象池中的Toast存放位置
private Queue<Toast> pool = new Queue<Toast>();
private void Awake()
{
Instance = this;
}
// 获取一个Toast
public Toast GetFromPool()
{
if (pool.Count > 0)
{
Toast toast = pool.Dequeue();
toast.gameObject.SetActive(true);
return toast;
}
return Instantiate(toastPrefab, poolParent);
}
// 回收Toast到对象池
public void ReturnToPool(Toast toast)
{
toast.Reset(); // 重置Toast状态
pool.Enqueue(toast); // 回收进队列
}
}