Files
Cielonos/Assets/Scripts/SLSFramework/LeanPoolAssistance/PooledObject.cs
SoulliesOfficial f7af60351b 阶段性完成
2025-12-08 05:27:53 -05:00

46 lines
1.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Lean.Pool;
using Sirenix.OdinInspector;
using UnityEngine;
namespace SLSFramework.LeanPoolAssistance
{
public class PooledObject : SerializedMonoBehaviour, IPoolable
{
[Tooltip("是否在生成后定时自动回收")]
public bool isAutoDespawn = true;
[ShowIf("isAutoDespawn")][Tooltip("自动回收时间")]
public float autoDespawnTime = 1;
private List<IPoolable> children;
private bool spawnExecuted = false;
public void OnSpawn()
{
if (spawnExecuted)
{
return;
}
spawnExecuted = true;
children = GetComponentsInChildren<IPoolable>().ToList();
children.Remove(this);
children.ForEach(child => child.OnSpawn());
if (isAutoDespawn)
{
LeanPool.Despawn(gameObject, autoDespawnTime);
}
}
public void OnDespawn()
{
spawnExecuted = false;
children.ForEach(child => child.OnDespawn());
}
}
}