using System.Collections.Generic;
using UnityEngine;
namespace XericUI.Core.StyleSheetUI
{
///
/// 标记可被全局刷新的样式组件。
/// 实现此接口的组件应在 OnEnable 调用 RegisterForStyleRefresh,
/// 在 OnDisable 调用 UnregisterForStyleRefresh。
///
public interface IStyleRefreshable
{
void RefreshStyle();
}
///
/// 全局样式刷新管理器——维护所有活跃的 IStyleRefreshable 组件。
/// 当外部样式表文件更新后,调用 ForceRefreshAll() 可一键刷新所有组件。
///
public static class StyleRefreshManager
{
private static readonly HashSet s_Components = new HashSet();
/// 注册一个样式组件
public static void Register(IStyleRefreshable component)
{
if (component != null)
s_Components.Add(component);
}
/// 注销一个样式组件
public static void Unregister(IStyleRefreshable component)
{
if (component != null)
s_Components.Remove(component);
}
///
/// 强制刷新所有已注册的样式组件。
/// 先调用 StyleManager.ForceRefresh() 同步样式表数据,
/// 再遍历所有组件重新读取并应用样式值。
///
public static void ForceRefreshAll()
{
#if XericLibrary
XericLibrary.Runtime.SuperStyleSheet.StyleManager.ForceRefresh();
#endif
int count = 0;
foreach (var c in s_Components)
{
try { c.RefreshStyle(); } catch { }
count++;
}
#if UNITY_EDITOR
Debug.Log($"[StyleRefresh] 已刷新 {count} 个样式组件。");
#endif
}
/// 当前注册的组件数
public static int RegisteredCount => s_Components.Count;
}
}