kakozuzo/Code/swb_base/extensions/GameObjectExtensions.cs
2024-10-30 19:01:58 +03:00

37 lines
759 B
C#

namespace SWB.Base;
public sealed class TimedDestroyComponent : Component
{
/// <summary>
/// How long until we destroy the GameObject.
/// </summary>
[Property] public float Time { get; set; } = 1f;
/// <summary>
/// The real time until we destroy the GameObject.
/// </summary>
[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<TimedDestroyComponent>();
component.Time = seconds;
}
}