com.alicizax.unity.framework/Runtime/UI/Manager/UIService.cs

180 lines
5.3 KiB
C#
Raw Normal View History

2025-09-05 19:46:30 +08:00
using System;
using AlicizaX;
using AlicizaX.Timer.Runtime;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace AlicizaX.UI.Runtime
{
2026-04-20 13:46:44 +08:00
internal sealed partial class UIService : ServiceBase, IUIService, IServiceTickable
2025-09-05 19:46:30 +08:00
{
private ITimerService _timerService;
2025-09-05 19:46:30 +08:00
protected override void OnInitialize()
2025-09-05 19:46:30 +08:00
{
}
protected override void OnDestroyService()
{
DestroyAllManagedUI();
}
void IServiceTickable.Tick(float deltaTime)
2025-09-05 19:46:30 +08:00
{
for (int layerIndex = 0; layerIndex < _openUI.Length; layerIndex++)
{
var layer = _openUI[layerIndex];
int count = layer.OrderList.Count;
2025-11-13 17:46:20 +08:00
if (count == 0) continue;
2025-09-05 19:46:30 +08:00
for (int i = 0; i < count; i++)
{
if (layer.OrderList.Count != count)
{
break;
}
var window = layer.OrderList[i];
2025-11-13 17:46:20 +08:00
if (window.MetaInfo.NeedUpdate)
window.View.InternalUpdate();
2025-09-05 19:46:30 +08:00
}
}
}
public UniTask<UIBase>? ShowUI(string type, params object[] userDatas)
{
if (UIMetaRegistry.TryGet(type, out var metaRegistry))
{
2025-11-17 16:55:06 +08:00
UIMetadata metadata = UIMetadataFactory.GetWindowMetadata(metaRegistry.RuntimeTypeHandle);
2025-09-05 19:46:30 +08:00
return ShowUI(metadata, userDatas);
}
return null;
}
2025-11-13 17:46:20 +08:00
public T ShowUISync<T>(params object[] userDatas) where T : UIBase
2025-09-05 19:46:30 +08:00
{
2025-11-17 16:55:06 +08:00
return (T)ShowUIImplSync(UIMetadataFactory.GetWindowMetadata<T>(), userDatas);
2025-09-05 19:46:30 +08:00
}
2025-11-13 17:46:20 +08:00
public async UniTask<T> ShowUI<T>(params System.Object[] userDatas) where T : UIBase
2025-09-05 19:46:30 +08:00
{
2025-11-17 16:55:06 +08:00
return (T)await ShowUIAsync(UIMetadataFactory.GetWindowMetadata<T>(), userDatas);
2025-09-05 19:46:30 +08:00
}
public void CloseUI<T>(bool force = false) where T : UIBase
{
2025-11-17 16:55:06 +08:00
CloseUIImpl(UIMetadataFactory.GetWindowMetadata<T>(), force).Forget();
2025-09-05 19:46:30 +08:00
}
public T GetUI<T>() where T : UIBase
{
2025-11-17 16:55:06 +08:00
return (T)GetUIImpl(UIMetadataFactory.GetWindowMetadata<T>());
2025-09-05 19:46:30 +08:00
}
private UniTask<UIBase> ShowUI(UIMetadata meta, params System.Object[] userDatas)
{
return ShowUIImplAsync(meta, userDatas);
}
private async UniTask<UIBase> ShowUIAsync(UIMetadata meta, params System.Object[] userDatas)
{
return await ShowUIImplAsync(meta, userDatas);
}
public void CloseUI(RuntimeTypeHandle handle, bool force = false)
{
2025-11-17 16:55:06 +08:00
var metadata = UIMetadataFactory.GetWindowMetadata(handle);
2025-09-05 19:46:30 +08:00
if (metadata.State != UIState.Uninitialized && metadata.State != UIState.Destroying)
{
CloseUIImpl(metadata, force).Forget();
}
}
void IUIService.SetTimerService(ITimerService timerService)
2025-09-05 19:46:30 +08:00
{
_timerService = timerService;
2025-09-05 19:46:30 +08:00
}
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;
}
2025-09-05 19:46:30 +08:00
}
}