首次提交
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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); // 回收进队列
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user