using System; using AlicizaX; using AlicizaX.Timer.Runtime; using Cysharp.Threading.Tasks; using UnityEngine; namespace AlicizaX.UI.Runtime { internal sealed partial class UIService : ServiceBase, IUIService, IServiceTickable { private ITimerService _timerService; protected override void OnInitialize() { } protected override void OnDestroyService() { DestroyAllManagedUI(); } void IServiceTickable.Tick(float deltaTime) { for (int layerIndex = 0; layerIndex < _openUI.Length; layerIndex++) { var layer = _openUI[layerIndex]; int count = layer.OrderList.Count; if (count == 0) continue; for (int i = 0; i < count; i++) { if (layer.OrderList.Count != count) { break; } var window = layer.OrderList[i]; if (window.MetaInfo.NeedUpdate) window.View.InternalUpdate(); } } } public UniTask? ShowUI(string type, params object[] userDatas) { if (UIMetaRegistry.TryGet(type, out var metaRegistry)) { UIMetadata metadata = UIMetadataFactory.GetWindowMetadata(metaRegistry.RuntimeTypeHandle); return ShowUI(metadata, userDatas); } return null; } public T ShowUISync(params object[] userDatas) where T : UIBase { return (T)ShowUIImplSync(UIMetadataFactory.GetWindowMetadata(), userDatas); } public async UniTask ShowUI(params System.Object[] userDatas) where T : UIBase { return (T)await ShowUIAsync(UIMetadataFactory.GetWindowMetadata(), userDatas); } public void CloseUI(bool force = false) where T : UIBase { CloseUIImpl(UIMetadataFactory.GetWindowMetadata(), force).Forget(); } public T GetUI() where T : UIBase { return (T)GetUIImpl(UIMetadataFactory.GetWindowMetadata()); } private UniTask ShowUI(UIMetadata meta, params System.Object[] userDatas) { return ShowUIImplAsync(meta, userDatas); } private async UniTask ShowUIAsync(UIMetadata meta, params System.Object[] userDatas) { return await ShowUIImplAsync(meta, userDatas); } public void CloseUI(RuntimeTypeHandle handle, bool force = false) { var metadata = UIMetadataFactory.GetWindowMetadata(handle); if (metadata.State != UIState.Uninitialized && metadata.State != UIState.Destroying) { CloseUIImpl(metadata, force).Forget(); } } void IUIService.SetTimerService(ITimerService timerService) { _timerService = timerService; } private void DestroyAllManagedUI() { for (int layerIndex = 0; layerIndex < _openUI.Length; layerIndex++) { LayerData layer = _openUI[layerIndex]; if (layer == null) { continue; } int count = layer.OrderList.Count; for (int i = count - 1; i >= 0; i--) { UIMetadata meta = layer.OrderList[i]; if (meta == null) { continue; } meta.CancelAsyncOperations(); meta.DisposeImmediate(); } layer.OrderList.Clear(); layer.IndexMap.Clear(); layer.LastFullscreenIndex = -1; } if (m_CacheWindow.Count > 0) { RuntimeTypeHandle[] handles = new RuntimeTypeHandle[m_CacheWindow.Count]; int writeIndex = 0; foreach (var pair in m_CacheWindow) { handles[writeIndex++] = pair.Key; } for (int i = 0; i < writeIndex; i++) { if (!m_CacheWindow.TryGetValue(handles[i], out CacheEntry entry)) { continue; } if (entry.TimerId > 0 && _timerService != null) { _timerService.RemoveTimer(entry.TimerId); } entry.Metadata.InCache = false; entry.Metadata.CancelAsyncOperations(); entry.Metadata.DisposeImmediate(); } m_CacheWindow.Clear(); } if (m_LastCountDownGuid != 0 && _timerService != null) { _timerService.RemoveTimer(m_LastCountDownGuid); m_LastCountDownGuid = 0; } if (m_LayerBlock != null) { UnityEngine.Object.Destroy(m_LayerBlock); m_LayerBlock = null; } UICacheLayer = null; UICanvasRoot = null; UICanvas = null; UICamera = null; UIRoot = null; } } }