43 lines
1.0 KiB
C#
43 lines
1.0 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using Lean.Pool;
|
|
|
|
namespace Invector
|
|
{
|
|
[vClassHeader("Destroy GameObject", openClose = false)]
|
|
public class vDestroyGameObject : vMonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public GameObject despawnObject;
|
|
public float delay = 1f;
|
|
public UnityEngine.Events.UnityEvent onDestroy;
|
|
|
|
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 (despawnObject != null)
|
|
{
|
|
LeanPool.Despawn(despawnObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
StartCoroutine(DestroyDelay());
|
|
}
|
|
}
|
|
} |