跳至主要内容

模塊介紹

重要 注意 提醒

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