by caitsithware » 2024/09/04 13:54
您可以使用 OutputExecutePort 创建一个 TaskComponent。
例如,创建如下所示的脚本。
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LogicToolkit;
[System.Serializable]
public class StepTask : TaskComponent, IPauseReceiver
{
[SerializeField]
[FieldPlacement(FieldPlacement.Top)]
private List<OutputExecutePort> _Steps;
private int _Index;
private OutputExecutePort _Current;
private TaskStatus _TaskStatus;
protected override void OnActivated()
{
_Current = _Steps[_Index];
_TaskStatus = TaskStatus.Running;
if (_Current == null || !_Current.Play(OnExit))
{
_TaskStatus = TaskStatus.Failure;
}
}
bool OnExit(OutputExecutePort execute, FlowExitNode exitNode)
{
_Index = (_Index + 1) % _Steps.Count;
if (exitNode == null
|| exitNode is ExitNode)
{
_TaskStatus = TaskStatus.Success;
return true;
}
return false;
}
protected override TaskStatus OnExecute()
{
_Current?.Update();
return _TaskStatus;
}
void IPauseReceiver.OnPause(bool pause)
{
if (pause)
{
_Current?.Pause();
}
else
{
_Current?.Resume();
}
}
}
- StepTask.png (55.86 KiB) Viewed 16283 times
您可以使用 OutputExecutePort 创建一个 TaskComponent。
例如,创建如下所示的脚本。
[code]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LogicToolkit;
[System.Serializable]
public class StepTask : TaskComponent, IPauseReceiver
{
[SerializeField]
[FieldPlacement(FieldPlacement.Top)]
private List<OutputExecutePort> _Steps;
private int _Index;
private OutputExecutePort _Current;
private TaskStatus _TaskStatus;
protected override void OnActivated()
{
_Current = _Steps[_Index];
_TaskStatus = TaskStatus.Running;
if (_Current == null || !_Current.Play(OnExit))
{
_TaskStatus = TaskStatus.Failure;
}
}
bool OnExit(OutputExecutePort execute, FlowExitNode exitNode)
{
_Index = (_Index + 1) % _Steps.Count;
if (exitNode == null
|| exitNode is ExitNode)
{
_TaskStatus = TaskStatus.Success;
return true;
}
return false;
}
protected override TaskStatus OnExecute()
{
_Current?.Update();
return _TaskStatus;
}
void IPauseReceiver.OnPause(bool pause)
{
if (pause)
{
_Current?.Pause();
}
else
{
_Current?.Resume();
}
}
}
[/code]
[attachment=0]StepTask.png[/attachment]