跳到主要内容

模块介绍

重要 注意 提醒

Coding Style wiki


基本说明

动作序列系统,能够自行定义 Action 并且自行组合运行组,默认 Actions 有 SequenceAction, ParallelAction, ParallelDelayAction, DelayAction, DelegateAction,另外如果针对动画需要进行拼凑处理,也可以使用 ActionSystem 作为运行。

  • 版本:v1.0.3 (com.michaelo.oxgkit.actionsystem)
  • 命名空间:OxGKit.ActionSystem

注意 本模块依赖 UniTaskOxGKit.LoggingSystem (需自行安装,详见 Installation),动作完成与移除的日志会通过 LoggingSystem 的 OxGKit.ActionSystem.Logger 日志器输出。


应用说明

核心概念

  • ActionRunner:动作运行器,通过 RunAction 启动动作或 QueueAction 排队动作,并由拥有者 (MonoBehaviour Update 等) 调用 DriveUpdate(dt) 驱动更新。
  • ActionBase:所有动作的抽象基类,通过重写 OnStart / OnUpdate / OnDone 实现自定义动作,并以 SetDurationMarkAsDone 控制完成时机。
  • Default Actions:默认动作,包含 SequenceAction (序列)、ParallelAction (并行)、ParallelDelayAction (交错并行)、DelayAction (延迟)、DelegateAction (委托回调),皆可互相嵌套组合。

运行流程

  1. 创建 ActionRunner (可命名以便日志识别)。
  2. 组合动作 (默认动作或自定义动作)。
  3. 通过 RunAction 启动动作 (或 QueueAction 排队追加)。
  4. 由拥有者于 Update 调用 DriveUpdate(Time.deltaTime) 驱动运行。
  5. 拥有者离场时调用 Release() 进行释放。

重要 运行器必须由单一拥有者驱动 DriveUpdate,没有驱动则动作不会前进。

注意 RunAction重置运行器再启动动作 (替换行为),若要追加动作请改用 QueueAction

脚本模板

  • 通过 Right-Click Create/OxGKit/Action System/Template Action.cs 实现自定义 Action。

提醒 自定义动作若 SetDuration(-1) 表示依条件结束,最后必须自行调用 MarkAsDone() 结束动作。


简单使用

驱动 ActionRunner

using OxGKit.ActionSystem;
using UnityEngine;

public class ActionDemo : MonoBehaviour
{
private ActionRunner _actionRunner;

private void Awake()
{
this._actionRunner = new ActionRunner("ActionRunner");
}

private void Update()
{
// Call by Main MonoBehaviour (Main Program)
this._actionRunner?.DriveUpdate(Time.deltaTime);
}

private void OnDestroy()
{
// 离场时释放运行器
this._actionRunner?.Release();
}
}

组合序列动作 (SequenceAction)

var seqAction = new SequenceAction("Colorful Sequence Callback Actions");

// 依序加入动作 (delayTime 大于 0 会先延迟再触发回调)
seqAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Seq] Hello Red"), 0f));
seqAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Seq] Hello Yellow"), 1f));
seqAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Seq] Hello Green"), 2f));

// 通过 ActionRunner 启动序列动作
this._actionRunner.RunAction(seqAction);

组合并行动作 (ParallelAction)

var parAction = new ParallelAction("Colorful Parallel Callback Actions");

// 加入动作 (同时执行)
parAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Par] Hello Red")));
parAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Par] Hello Yellow")));
parAction.AddAction(DelegateAction.CreateDelegateAction(() => Debug.Log("[Par] Hello Green")));

// 通过 ActionRunner 启动并行动作
this._actionRunner.RunAction(parAction);

实现自定义 Action

using OxGKit.ActionSystem;

public class NewTplAction : ActionBase
{
#region Default Constructor
public NewTplAction()
{
this.name = nameof(NewTplAction);
}

public NewTplAction(int uid) : this()
{
this.uid = uid;
}
#endregion

protected override void OnStart()
{
// Set -1 is end by condition or duration time (-1 = have to MarkAsDone manually)
this.SetDuration(-1);

// Do somethings...

// And then if SetDuration(-1), the end must invoke MarkAsDone
this.MarkAsDone();
}
}

[参考 Example]


Installation

Install via git URL
Add https://github.com/michael811125/OxGKit.git?path=Assets/OxGKit/ActionSystem/Scripts to Package Manager

第三方库 (需自行安装)

Samples (Package Manager -> Samples)


模块 API


Demo

ActionSystem Demo