namespace SWB.Base;
public sealed class TimedDestroyComponent : Component
{
///
/// How long until we destroy the GameObject.
///
[Property] public float Time { get; set; } = 1f;
///
/// The real time until we destroy the GameObject.
///
[Property, ReadOnly] TimeUntil TimeUntilDestroy { get; set; } = 0;
protected override void OnStart()
{
TimeUntilDestroy = Time;
}
protected override void OnUpdate()
{
if ( TimeUntilDestroy )
{
GameObject.Destroy();
}
}
}
public static class GameObjectExtensions
{
public static void DestroyAsync( this GameObject self, float seconds = 1.0f )
{
var component = self.Components.Create();
component.Time = seconds;
}
}