Files
beyond/Assets/Scripts/InvectorDerivatives/DespawnGameObject.cs
2024-11-20 15:21:28 +01:00

41 lines
995 B
C#

using UnityEngine;
using System.Collections;
using Invector;
using Lean.Pool;
namespace Beyond
{
[vClassHeader("Destroy GameObject", openClose = false)]
public class DespawnGameObject : vMonoBehaviour
{
public float delay = 1f;
public UnityEngine.Events.UnityEvent onDestroy;
public bool m_returnToPool = true;
private float timeOffset = 1f;
IEnumerator DestroyDelay()
{
yield return new WaitForSeconds(delay + timeOffset);
AudioSource audio = GetComponent<AudioSource>();
if (audio != null)
{
yield return new WaitUntil(() => !audio.isPlaying);
}
onDestroy?.Invoke();
if (m_returnToPool)
LeanPool.Despawn(gameObject);
else
{
Destroy(gameObject);
}
}
void OnEnable()
{
StartCoroutine(DestroyDelay());
}
}
}