MonoObj Lifecycle

SkillMedia

Guide to the MonoObj update lifecycle system. Use when you need to: (1) understand the WorldUpdateSimulator update loop architecture (2) implement per-frame update logic such as Simulate and Render (3) add IUpdateSimulate, IBeforeSimulate, IAfterSimulate, IRenderUpdate implementations (4) understand

Available today. Use it from your connected AI after setup.

Add ahel to your AI once: Claude, ChatGPT, Cursor, Claude Code or Codex. Then ask it to use this.

Then ask your AI: use the MonoObj Lifecycle skill

What this skill tells your AI

The instructions your AI receives, as published by red-candle-games-co-ltd/monofsm in skills/MonoObjLifecycle/SKILL.md and read by ahel’s review.

MonoObj 的每幀更新透過 WorldUpdateSimulator 集中管理,以介面驅動方式分階段執行。

執行時序

LocalSimulatorRunner.FixedUpdate / FusionSimulatorRunner.BeforeTick + FixedUpdateNetwork
├── World.BeforeSimulate(...)               → IBeforeSimulate
├── World.Simulate(...)                     → IUpdateSimulate
└── World.AfterSimulate(...)                → IAfterSimulate

LocalSimulatorRunner.LateUpdate / FusionSimulatorRunner.Render
└── World.Render(...)                       → IRenderUpdate

LocalSimulatorRunner.LateUpdate 尾段 / FusionSimulatorRunner.AfterRender
└── World.AfterRender()                     → IAfterRenderMono

每顆註冊的 MonoObj 都是獨立 scope。[AutoChildren(StopAtType = typeof(MonoObj))] 不會跨進 nested MonoObj;nested MonoObj 會自己註冊並被 WorldUpdateSimulator 呼叫,不是由 root 遞迴代跑。

初始化時序:ISceneAwake / ISceneStart

一次性初始化不要寫在 Unity 的 Awake() / Start(),改用這兩個介面(都在 global namespace, 定義於 MonoFSM/1_MonoFSM_Core/Runtime/Entity/IResetter.cs):

介面方法時機
ISceneAwakeEnterSceneAwake()由 WorldUpdateSimulator.WorldInit() 分派,早於 ISceneStart
ISceneStartEnterSceneStart()WorldInit 後段,此時 MonoDict 等系統已 prepared

WorldInit 的觸發點:單機是 LocalSimulatorRunner.Start();Fusion 是 FusionSimulatorRunner.OnSceneLoadDone()(幾乎鐵定晚於一般 Unity Start())。

坑 1:ISceneAwake 只由 MonoObj 對自己子樹分派

MonoObj(MonoFSM/1_MonoFSM_Core/Runtime/LifeCycle/Update/MonoObj.cs)是對自己的 GetComponentsInChildren<ISceneAwake> 分派;SceneLifecycleManager.HandleGameLevelAwake(level) 同樣只掃指定 level 物件的子樹。

→ 直接在 scene 建一顆裸 root GameObject 掛 ISceneAwake 元件,EnterSceneAwake() 永遠不會被呼叫, 而且完全沒有錯誤訊息。症狀是「元件欄位都填好了但初始化沒發生」(例如 static 注入仍是 null)。

做法:這類「場景放一顆就好」的 installer 要掛在某個帶 MonoObj 的節點子樹下(本專案用 FusionFPS Core/GameCore)。除錯時先檢查它要寫的目標值是否真的被設,再往上確認 parent 有沒有 MonoObj。

MonoObj._sceneStarts 是 [AutoChildren] 的 interface array,runtime 由 AutoAttributeManager 重抓, 改完不需要重存 prefab(除非該 prefab 有 PrefabSerializeCache)。

坑 2:在 Start() 裡呼叫 GetVar 會靜默拿到 null

MonoDict.Get(key)(MonoFSM/1_MonoFSM_Core/Runtime/0_Pattern/MonoDict.cs)在 _isPrepared == false && Application.isPlaying 時只印一行 GetFrom {key} Dict, Not prepared 就回 default。 _isPrepared 要等 MonoDict.EnterSceneAwake(),也就是 WorldInit 之後才成立。

→ 任何在 Unity Start() 裡呼叫 entity.GetVar(tag) / VariableFolder.GetVariable(...) 的程式碼都不保證拿得到東西。 危險的是常見的過濾寫法 RemoveAll(e => e.GetVar(tag) == null):整份清單被清空,又只 cache 一次不會重抓 —— Console 只留一行 error,遊戲端是靜默空資料,很難聯想到時序問題。

做法:「一次性抓取 + 快取」的 component 一律實作 ISceneStart.EnterSceneStart(),不要用 Start() (AbstractDescriptionBehaviour.Start() 本身就標了 //FIXME: 不該用這個?)。前提同樣是該 component 要在有 MonoObj 的子樹下(見坑 1)。

更新介面

所有介面定義於 MonoFSM/1_MonoFSM_Core/Runtime/LifeCycle/Update/Simulate/IUpdateSimulate.cs。

介面方法時機用途
IBeforeSimulateBeforeSimulate(float deltaTime)FixedUpdate 開頭輸入處理、前置計算
IUpdateSimulateSimulate(float deltaTime)FixedUpdate 主體核心模擬邏輯
IAfterSimulateAfterSimulate(float deltaTime)FixedUpdate 尾段後處理、同步
IRenderUpdateRender(float runnerLocalRenderTime)Local LateUpdate / Fusion Render視覺更新、插值、動畫;不吃 ShouldSimulte authority gate
IAfterRenderMonoAfterRender()Fusion AfterRender / local render 尾段必須晚於一般 Render 的視覺收尾

實作模式

實作任一介面並掛在 MonoObj 子物件上,會被 [AutoChildren] 自動收集。

public class MyVisualUpdater : MonoBehaviour, IRenderUpdate
{
    public void Render(float deltaTime)
    {
        // 視覺插值、動畫更新等
    }
}
public class MySimulator : MonoBehaviour, IUpdateSimulate
{
    public void Simulate(float deltaTime)
    {
        // 核心模擬邏輯
    }

    // 可選:控制執行順序(數字越小越先)
    public int SimulateOrder => 10;
}

關鍵檔案

檔案職責
WorldUpdateSimulator.cs世界更新中心,管理所有 MonoObj 的註冊與每幀迭代
LocalSimulatorRunner.cs本地模式的 Runner,驅動 FixedUpdate/LateUpdate
MonoObj.cs持有各階段介面陣列,轉發更新呼叫
IUpdateSimulate.cs所有更新介面定義

Culling phase gate

MonoObj 支援三種 handle,三者都由自己 scope 內的 [AutoChildren(StopAtType = MonoObj)] 自動收集:

Handle停止的 phase用途
CullingActiveHandleSimulate + Render 全部legacy 相容;只有真的要整顆暫停才用
SimulationCullingActiveHandleBeforeSimulate / Simulate / AfterSimulate距離型 gameplay、AI、sensor 成本控制
RenderCullingActiveHandleRender / AfterRenderRenderer、VFX、Animator 等本機視覺成本控制

MonoObj.IsCulling 為既有 gameplay 相容介面,等同 IsSimulationCulling。Render 專用判斷看 IsRenderCulling。parent 的 simulation/render culling 會分 phase 傳給 nested MonoObj; _isIgnoreParentObjCulling 會同時切斷兩種 parent phase 繼承。

共用 module Packages/com.monofsm.pro/Prefabs/Prefab Modules/Culling Event Target.prefab 的標準串法:

  • NearOnly → SimulationCullingActiveHandle
  • Visible OR Near → RenderCullingActiveHandle

CullingEventTarget、root MonoObj、NetworkObject / NetworkBehaviour、同步用 Var/FSM 與 RenderLoopHandler 本身留在 always-on shell,不要放進被 CullingTargetGameObjects 關閉的 root。 CullingGroup visibility 是各 peer 的 camera-local 結果,只能控制本機 scheduling/visual,不能拿來改 network state 或 authority。

注意事項

項目說明
nested MonoObj 獨立更新每顆 nested MonoObj 都要註冊;StopAtType 讓各 scope 不重複收集 loop component
Proxy 的 logic / visual 分流Simulate 由 ShouldSimulte(State/Input Authority)擋;Render 不吃 authority gate,所以 non-simulated proxy 也能更新本機視覺
IsReady 檢查WorldUpdateSimulator 在 WorldInit() 後才開始更新
TimeScale透過 WorldUpdateSimulator.DeltaTime 取得含 TimeScale 的 deltaTime
Simulate vs Renderlocal 為 FixedUpdate/LateUpdate;Fusion 為 FixedUpdateNetwork/Render。不要把 Render 寫成依賴 local camera 的 authoritative logic

Signals

GitHub stars
25
Forks
2
Last commit
Sep 2026
Advanced
Item type
skill
Key
monoobjlifecycle
Source
github.com/red-candle-games-co-ltd/monofsm