Files
GoakeFlax/Source/Game/Audio/AudioSourceDelayed.cs
2024-04-06 14:42:10 +03:00

47 lines
1.0 KiB
C#

using FlaxEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game;
public class AudioSourceDelayed : AudioSource
{
public float Delay = 0f;
public override void OnBeginPlay()
{
if (Delay >= 0f)
{
PlayOnStart = false;
var script = AddScript<AudioSourceDelayedScript>();
script.PlayStartTime = Delay + FlaxEngine.Time.GameTime;
}
base.OnBeginPlay();
}
}
public class AudioSourceDelayedScript : Script
{
[NoSerialize]
[HideInEditor]
public float PlayStartTime = 0f;
public override void OnStart()
{
FlaxEngine.Object.Destroy(Actor, Actor.As<AudioSourceDelayed>().Clip.Length + Actor.As<AudioSourceDelayed>().Delay);
}
public override void OnFixedUpdate()
{
if (FlaxEngine.Time.GameTime >= PlayStartTime)
{
PlayStartTime = float.MaxValue;
Actor.As<AudioSourceDelayed>().Play();
}
}
}