修复部分可能矫枉过正的enable处理流程。

在所有生命周期事件外添加try块避免失败
This commit is contained in:
2026-06-16 17:52:49 +08:00
parent 8dfcaba43a
commit 7c9369a300
2 changed files with 62 additions and 27 deletions
+43 -15
View File
@@ -3,6 +3,7 @@ using UnityEngine;
using UnityEngine.UI;
using XericLibrary.Runtime.MacroLibrary;
using XericLibraryEditor.Debug;
using System;
namespace XericUI.VisualForm
{
@@ -271,27 +272,36 @@ namespace XericUI.VisualForm
protected virtual void LateUpdate()
{
// 0. 处理待注册锚点(在OnEnable阶段未找到坞的锚点)
ProcessPendingAnchors();
try { ProcessPendingAnchors(); }
catch (Exception ex) { Debug.LogException(ex); }
// 1. 处理锚点脏更新(新增/移除窗口)
ProcessDirtyAnchors();
try { ProcessDirtyAnchors(); }
catch (Exception ex) { Debug.LogException(ex); }
// 2. 检查摄像机变化
CheckCameraDirty();
try { CheckCameraDirty(); }
catch (Exception ex) { Debug.LogException(ex); }
// 3. 检查所有中间类的位置脏(必须在碰撞钩子之前,供碰撞判断是否需要计算)
foreach (var kvp in _activeStates)
try
{
var state = kvp.Value;
if (state == null || !state.IsEnabled) continue;
state.CheckPositionDirty(m_PositionThreshold);
foreach (var kvp in _activeStates)
{
var state = kvp.Value;
if (state == null || !state.IsEnabled) continue;
state.CheckPositionDirty(m_PositionThreshold);
}
}
catch (Exception ex) { Debug.LogException(ex); }
// 4. 碰撞分离计算(子类重写钩子,基类为空)
OnBeforePositionUpdates();
try { OnBeforePositionUpdates(); }
catch (Exception ex) { Debug.LogException(ex); }
// 5. 遍历所有中间类,更新坐标
UpdateAllStatePositions();
try { UpdateAllStatePositions(); }
catch (Exception ex) { Debug.LogException(ex); }
// 6. 复位脏标记
_cameraDirty = false;
@@ -318,7 +328,8 @@ namespace XericUI.VisualForm
bool needUpdate = _cameraDirty || state.CoordinateDirty;
if (needUpdate)
{
UpdateStatePosition(state);
try { UpdateStatePosition(state); }
catch (Exception ex) { Debug.LogException(ex); }
}
}
}
@@ -397,8 +408,12 @@ namespace XericUI.VisualForm
var anchor = _pendingRemoves[i];
if (_activeStates.TryGetValue(anchor, out var state))
{
state.OnDisable();
state.OnEnd();
try { state.OnDisable(); }
catch (Exception ex) { Debug.LogException(ex); }
try { state.OnEnd(); }
catch (Exception ex) { Debug.LogException(ex); }
_statePool.Enqueue(state);
_activeStates.Remove(anchor);
}
@@ -416,12 +431,24 @@ namespace XericUI.VisualForm
if (_activeStates.ContainsKey(anchor)) continue;
var state = GetPooledState();
state.OnStart(anchor, this);
try { state.OnStart(anchor, this); }
catch (Exception ex)
{
Debug.LogException(ex);
// OnStart 失败时回收到池中,避免泄漏
_statePool.Enqueue(state);
continue;
}
_activeStates[anchor] = state;
// 如果锚点当前处于激活状态,调用Enable
if (anchor.isActiveAndEnabled)
state.OnEnable();
{
try { state.OnEnable(); }
catch (Exception ex) { Debug.LogException(ex); }
}
}
_pendingAdds.Clear();
}
@@ -551,7 +578,8 @@ namespace XericUI.VisualForm
if (entryOverflow != entry.CurrentOverflow)
{
entry.CurrentOverflow = entryOverflow;
anchorWindow?.OnWindowLevelChange(i, entryOverflow);
try { anchorWindow?.OnWindowLevelChange(i, entryOverflow); }
catch (Exception ex) { Debug.LogException(ex); }
}
var target = entry.Instance.transform;
+19 -12
View File
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using System;
namespace XericUI.VisualForm
{
@@ -141,17 +142,12 @@ namespace XericUI.VisualForm
}
// 通知条目实例已创建(无论是新实例化还是从池中复用的)
entry.OnInstanceCreated(this, entry.Instance);
try { entry.OnInstanceCreated(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
// 将WindowState引用注入条目,使其能通过MarkDirty通知坞
entry.State = this;
// 立即按entry配置的Enabled决定实例的active状态。
// OnInstanceCreated可能已修改Enabled为false,必须在此处应用,
// 避免实例在后续OnEnable中被跳过激活但保持active的问题。
if (entry.Instance != null)
entry.Instance.SetActive(entry.Enabled);
Entries.Add(entry);
}
@@ -177,7 +173,10 @@ namespace XericUI.VisualForm
bool shouldActive = entry.Enabled;
entry.Instance.SetActive(shouldActive);
if (shouldActive)
entry.OnInstanceEnable(this, entry.Instance);
{
try { entry.OnInstanceEnable(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
}
}
}
@@ -198,7 +197,8 @@ namespace XericUI.VisualForm
if (entry.Instance != null)
{
entry.Instance.SetActive(false);
entry.OnInstanceDisable(this, entry.Instance);
try { entry.OnInstanceDisable(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
}
}
}
@@ -213,7 +213,8 @@ namespace XericUI.VisualForm
{
if (entry.IsPrefab && entry.Instance != null)
{
entry.OnInstanceRecycle(this, entry.Instance);
try { entry.OnInstanceRecycle(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
WindowPool.Release(entry.WindowObject, entry.Instance);
}
@@ -281,9 +282,15 @@ namespace XericUI.VisualForm
entry.Instance.SetActive(enabled);
if (enabled && !wasActive)
entry.OnInstanceEnable(this, entry.Instance);
{
try { entry.OnInstanceEnable(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
}
else if (!enabled && wasActive)
entry.OnInstanceDisable(this, entry.Instance);
{
try { entry.OnInstanceDisable(this, entry.Instance); }
catch (Exception ex) { Debug.LogException(ex); }
}
}
#region 碰撞偏移管理