com.alicizax.unity.ui.exten.../Runtime/RecyclerView/ObjectPool/UnityMixedComponentFactory.cs
陈思海 dc8c840d69 RecyclerView 大优化
优化RecycleView 渲染架构
优化RecyclerView 渲染性能 增加 缓存 异步
增加Navagation导航锚点相关
2026-03-31 15:18:50 +08:00

73 lines
1.9 KiB
C#

namespace AlicizaX.UI
{
using System.Collections.Generic;
using UnityEngine;
public class UnityMixedComponentFactory<T> : IMixedObjectFactory<T> where T : Component
{
protected T template;
protected Transform parent;
protected List<T> list;
private Dictionary<string, T> dict = new();
public UnityMixedComponentFactory(T template, Transform parent)
{
this.template = template;
this.parent = parent;
}
public UnityMixedComponentFactory(List<T> list, Transform parent)
{
this.list = list;
this.parent = parent;
foreach (var data in list)
{
dict[data.name] = data;
}
}
public UnityMixedComponentFactory(Dictionary<string, T> dict, Transform parent)
{
this.dict = dict;
this.parent = parent;
}
public T Create(string typeName)
{
T obj = Object.Instantiate(dict[typeName], parent);
if (obj.transform is RectTransform rectTransform)
{
rectTransform.anchoredPosition3D = Vector3.zero;
rectTransform.localRotation = Quaternion.identity;
rectTransform.localScale = Vector3.one;
}
else
{
obj.transform.localPosition = Vector3.zero;
obj.transform.localRotation = Quaternion.identity;
obj.transform.localScale = Vector3.one;
}
return obj;
}
public void Destroy(string typeName, T obj)
{
Object.Destroy(obj.gameObject);
}
public void Reset(string typeName, T obj)
{
obj.gameObject.SetActive(false);
}
public bool Validate(string typeName, T obj)
{
return true;
}
}
}