Files
A-Fairytale-Gone-Bad-Briar-…/Assets/Scripts/Framework/Managers/Assets/Components/NotifyOnDestruction.cs

44 lines
990 B
C#

using System;
using BriarQueen.Framework.Services.Destruction;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace BriarQueen.Framework.Managers.Assets.Components
{
public class NotifyOnDestruction : MonoBehaviour, IDestructible
{
private bool _destroyedNotified;
public UniTask OnPreDestroy()
{
OnPreDestroyCalled?.Invoke();
return UniTask.CompletedTask;
}
public UniTask OnDestroyed()
{
RaiseDestroyedOnce();
return UniTask.CompletedTask;
}
public event Action OnPreDestroyCalled;
public event Action OnDestroyedCalled;
private void OnDestroy()
{
RaiseDestroyedOnce();
}
private void RaiseDestroyedOnce()
{
if (_destroyedNotified)
{
return;
}
_destroyedNotified = true;
OnDestroyedCalled?.Invoke();
}
}
}