41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
||
using SoulliesFramework.General;
|
||
using UniRx;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
namespace Continentis.MainGame.Commands
|
||
{
|
||
public class Cmd_Function : CommandBase
|
||
{
|
||
private readonly float functionDuration;
|
||
private readonly UnityAction function;
|
||
private readonly bool executeAtStart;
|
||
|
||
public Cmd_Function(float functionDuration, UnityAction function, bool executeAtStart = true)
|
||
{
|
||
this.functionDuration = functionDuration;
|
||
this.function = function;
|
||
this.executeAtStart = executeAtStart;
|
||
}
|
||
|
||
protected override IObservable<Unit> OnExecute(CommandContext context)
|
||
{
|
||
if (functionDuration > 0)
|
||
{
|
||
if (executeAtStart) //在持续时间开始时执行
|
||
{
|
||
function?.Invoke();
|
||
return Observable.Timer(TimeSpan.FromSeconds(functionDuration)).AsUnitObservable();
|
||
}
|
||
|
||
return Observable.Timer(TimeSpan.FromSeconds(functionDuration))
|
||
.Do(_ => function?.Invoke())
|
||
.AsUnitObservable(); //在持续时间结束时执行
|
||
}
|
||
|
||
function?.Invoke();
|
||
return Observable.Return(Unit.Default); //如果持续时间为0,立即完成
|
||
}
|
||
}
|
||
} |