com.alicizax.unity.ui.exten.../Runtime/RecyclerView/ObjectPool/UnityMixedComponentFactory.cs

73 lines
1.9 KiB
C#
Raw Normal View History

2025-12-26 14:22:46 +08:00
namespace AlicizaX.UI
2025-05-28 19:37:38 +08:00
{
using System.Collections.Generic;
using UnityEngine;
2025-12-26 14:22:46 +08:00
public class UnityMixedComponentFactory<T> : IMixedObjectFactory<T> where T : Component
2025-05-28 19:37:38 +08:00
{
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;
}
2025-05-28 19:37:38 +08:00
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;
}
}
}