diff --git a/Runtime/VisualForm/WindowEntry.cs b/Runtime/VisualForm/WindowEntry.cs index 1ac5525..0789e1e 100644 --- a/Runtime/VisualForm/WindowEntry.cs +++ b/Runtime/VisualForm/WindowEntry.cs @@ -75,5 +75,47 @@ namespace XericUI.VisualForm internal void SetEnabledInternal(bool value) => m_Enabled = value; internal void SetClampToSafeZoneInternal(bool value) => m_ClampToSafeZone = value; + + // --- 实例生命周期虚方法(重写以自定义窗口行为) --- + + /// + /// 实例创建后回调 — 在实例从对象池取出或新实例化后调用。 + /// 可重写以执行额外初始化(如获取组件引用、注册事件)。 + /// + /// 当前的WindowState中间类(提供锚点、坞、溢出状态等上下文) + /// 新创建/复用的GameObject实例 + public virtual void OnInstanceCreated(WindowState state, GameObject instance) { } + + /// + /// 实例激活回调 — 在锚点Enable、窗口条目应当显示时调用。 + /// 可重写以实现自定义激活逻辑。 + /// + /// 当前的WindowState中间类 + /// 当前的实例GameObject(可能是原型对象或预制体实例) + public virtual void OnInstanceEnable(WindowState state, GameObject instance) { } + + /// + /// 实例隐藏回调 — 在锚点Disable、窗口条目应当隐藏时调用。 + /// 可重写以实现自定义隐藏逻辑。 + /// + /// 当前的WindowState中间类 + /// 当前的实例GameObject + public virtual void OnInstanceDisable(WindowState state, GameObject instance) { } + + /// + /// 实例回收回调 — 在实例回收到对象池时调用。 + /// 可重写以清理状态、移除事件监听等。 + /// + /// 当前的WindowState中间类 + /// 即将被回收的实例GameObject + public virtual void OnInstanceRecycle(WindowState state, GameObject instance) { } + + /// + /// 实例销毁回调 — 在实例即将被Destroy时调用。 + /// 可重写以执行最终清理(通常情况下回收到对象池足矣)。 + /// + /// 当前的WindowState中间类 + /// 即将被销毁的实例GameObject + public virtual void OnInstanceDestroy(WindowState state, GameObject instance) { } } } diff --git a/Runtime/VisualForm/WindowState.cs b/Runtime/VisualForm/WindowState.cs index 2331447..4fbb931 100644 --- a/Runtime/VisualForm/WindowState.cs +++ b/Runtime/VisualForm/WindowState.cs @@ -121,6 +121,9 @@ namespace XericUI.VisualForm entry.Instance = entry.WindowObject; } + // 通知条目实例已创建(无论是新实例化还是从池中复用的) + entry.OnInstanceCreated(this, entry.Instance); + Entries.Add(entry); } @@ -142,7 +145,10 @@ namespace XericUI.VisualForm foreach (var entry in Entries) { if (entry.Instance != null && entry.Enabled) + { entry.Instance.SetActive(true); + entry.OnInstanceEnable(this, entry.Instance); + } } // 激活时强制标记坐标脏,确保下一帧更新位置 @@ -159,7 +165,10 @@ namespace XericUI.VisualForm foreach (var entry in Entries) { if (entry.Instance != null) + { entry.Instance.SetActive(false); + entry.OnInstanceDisable(this, entry.Instance); + } } } @@ -172,7 +181,10 @@ namespace XericUI.VisualForm foreach (var entry in Entries) { if (entry.IsPrefab && entry.Instance != null) + { + entry.OnInstanceRecycle(this, entry.Instance); WindowPool.Release(entry.WindowObject, entry.Instance); + } entry.Instance = null; entry.IsPrefab = false;