com.alicizax.unity.framework/Runtime/UI/Constant/UIMetadataFactory.cs
陈思海 6397cc03b2 框架大更新
1.进步优化UI系统 加载问题 性能问题 Canvas重绘问题 边界处理问题
2.优化对象池和游戏对象池的性能 游戏对象池根据窗口 策略定期清理
3.优化整个AppService 和ServiceWorld结构 固定三大类 具体参考代码
2026-03-31 17:25:20 +08:00

84 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using AlicizaX;
using AlicizaX.ObjectPool;
namespace AlicizaX.UI.Runtime
{
internal static class UIMetadataFactory
{
private static readonly Dictionary<RuntimeTypeHandle, UIMetadata> UIWindowMetadata = new();
private static readonly IObjectPool<UIMetadataObject> m_UIMetadataPool;
static UIMetadataFactory()
{
m_UIMetadataPool = AppServices.Require<IObjectPoolService>().CreatePool<UIMetadataObject>(
new ObjectPoolCreateOptions(
name: "UI Metadata Pool",
allowMultiSpawn: false,
autoReleaseInterval: 60,
capacity: 16,
expireTime: 60f,
priority: 0));
}
internal static UIMetadata GetWindowMetadata<T>()
{
return GetWindowMetadata(typeof(T).TypeHandle);
}
internal static UIMetadata GetWindowMetadata(RuntimeTypeHandle handle)
{
if (!UIWindowMetadata.TryGetValue(handle, out var meta))
{
meta = new UIMetadata(Type.GetTypeFromHandle(handle));
UIWindowMetadata[handle] = meta;
}
return meta;
}
internal static UIMetadata GetWidgetMetadata<T>()
{
return GetWidgetMetadata(typeof(T).TypeHandle);
}
internal static UIMetadata GetWidgetMetadata(RuntimeTypeHandle handle)
{
return GetFromPool(Type.GetTypeFromHandle(handle));
}
private static UIMetadata GetFromPool(Type type)
{
if (type == null) return null;
string typeHandleKey = type.FullName;
UIMetadataObject metadataObj = m_UIMetadataPool.Spawn(typeHandleKey);
if (metadataObj != null && metadataObj.Target != null)
{
return (UIMetadata)metadataObj.Target;
}
UIMetadata newMetadata = new UIMetadata(type);
UIMetadataObject newMetadataObj = UIMetadataObject.Create(newMetadata, typeHandleKey);
m_UIMetadataPool.Register(newMetadataObj, true);
return newMetadata;
}
internal static void ReturnToPool(UIMetadata metadata)
{
if (metadata == null) return;
m_UIMetadataPool.Unspawn(metadata);
}
}
}