修改锚点窗口对entry类的依赖,现在可以自行通过泛型扩展。

This commit is contained in:
2026-06-15 09:26:24 +08:00
parent 856c395819
commit 922eb0e450
11 changed files with 394 additions and 26 deletions
+74 -8
View File
@@ -1,24 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace XericUI.VisualForm
{
/// <summary>
/// 锚点窗口
/// </summary>
[AddComponentMenu("Xeric UI Vessel/AnchorWindow/AnchorWindow")]
public class AnchorWindowByDefaultEntry : AnchorWindow<WindowEntry> { }
/// <summary>
/// 锚点窗口 - WorldAnchor的子类,持有一组UI窗口条目的引用。
/// 每个条目包含窗口对象、屏幕偏移量和激活状态,支持一个锚点同时生成多个定位窗口。
/// 窗口对象可以是实际的场景对象,也可以是预制体。
/// </summary>
[AddComponentMenu("Xeric UI Vessel/AnchorWindow/AnchorWindow")]
public class AnchorWindow : WorldAnchor
public abstract class AnchorWindow<T> : WorldAnchor, IAnchorWindowEntry<T>
where T : WindowEntry
{
[Header("窗口条目")]
[SerializeField, Tooltip("窗口对象集合,每个条目可独立配置偏移和启用状态")]
private List<WindowEntry> m_WindowEntries = new List<WindowEntry>();
private List<T> m_WindowEntries = new List<T>();
/// <summary>
/// 窗口条目列表
/// </summary>
public List<WindowEntry> WindowEntries => m_WindowEntries;
public IList<T> WindowEntries => m_WindowEntries;
public IEnumerable<WindowEntry> DefaultTypeWindowEntries => m_WindowEntries;
public int DefaultTypeWindowEntriesCount => m_WindowEntries.Count;
/// <summary>
/// 当前生效的窗口条目数(过滤掉WindowObject为null的条目)
@@ -36,8 +45,9 @@ namespace XericUI.VisualForm
}
/// <summary>
/// 获取第一个有效窗口对象的Transform(兼容旧API)
/// 获取第一个有效窗口对象的Transform
/// </summary>
[Obsolete("旧api")]
public Transform GetWindowTransform()
{
foreach (var entry in m_WindowEntries)
@@ -50,6 +60,50 @@ namespace XericUI.VisualForm
#region 工具方法
/// <summary>
/// 设置指定索引条目的启用状态。写入后自动检查所有条目,
/// 若全部禁用则同步标记到 WindowState(坞将跳过该窗口的位置更新)。
/// </summary>
/// <param name="index">条目索引</param>
/// <param name="enabled">是否启用</param>
public void SetEntryEnabled(int index, bool enabled)
{
if (index < 0 || index >= m_WindowEntries.Count) return;
var entry = m_WindowEntries[index];
if (entry == null) return;
entry.SetEnabledInternal(enabled);
// 同步到WindowState
GetWindowState()?.RefreshFlags();
}
public void SetAllEntryEnabled(bool enabled)
{
for (int i = 0; i < m_WindowEntries.Count; i++)
m_WindowEntries[i].SetEnabledInternal(enabled);
GetWindowState()?.RefreshFlags();
}
/// <summary>
/// 设置指定索引条目的钳制到安全区标记。写入后自动同步 WindowState 的 HasAnyClamp。
/// </summary>
/// <param name="index">条目索引</param>
/// <param name="clamp">是否钳制</param>
public void SetEntryClampToSafeZone(int index, bool clamp)
{
if (index < 0 || index >= m_WindowEntries.Count) return;
var entry = m_WindowEntries[index];
if (entry == null) return;
entry.SetClampToSafeZoneInternal(clamp);
// 同步到WindowState
GetWindowState()?.RefreshFlags();
}
/// <summary>
/// 从所属锚点坞获取自己的中间窗口状态类
/// </summary>
@@ -118,5 +172,17 @@ namespace XericUI.VisualForm
}
#endregion
#region 生命周期事件
/// <summary>
/// 窗口层级变更事件 — 当某个条目的溢出状态发生变化时触发。
/// 继承类可重写此方法,根据溢出状态切换不同窗口显示(如溢出后切换为指向示意器)。
/// </summary>
/// <param name="entryIndex">发生变化的条目索引</param>
/// <param name="state">新的溢出状态</param>
public virtual void OnWindowLevelChange(int entryIndex, OverflowState state) { }
#endregion
}
}