Files
aibis-dream/Assets/Scripts/UI/DialogUI/Bubbles/BubbleFactory.cs
T
2025-06-05 22:02:26 +08:00

50 lines
1.4 KiB
C#

using System.Collections.Generic;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using UnityEngine;
namespace AibisDream
{
public class BubbleFactory
{
private readonly Dictionary<PivotType, SimpleObjectPool<Bubble>> _poolDict = new();
private readonly Transform _bubbleRoot;
public BubbleFactory(Transform bubbleRoot)
{
_bubbleRoot = bubbleRoot;
}
public Bubble Create(BubbleSlotData data)
{
var pool = TryGetPool(data.pivotType);
var bubble = pool.Allocate();
bubble.OnDisposed += o => pool.Recycle(o);
bubble.LoadConfig(data);
return bubble;
}
private SimpleObjectPool<Bubble> TryGetPool(PivotType pivotType)
{
if (_poolDict.TryGetValue(pivotType, out var pool))
{
return pool;
}
var prefab =
ResourceKit.LoadAssetSync<GameObject>(string.Format(ConstRef.BubblePrefab, pivotType.ToString()));
var newPool = new SimpleObjectPool<Bubble>(() => InstantiateBubble(prefab));
_poolDict.Add(pivotType, newPool);
return newPool;
}
private Bubble InstantiateBubble(GameObject prefab)
{
return Object.Instantiate(prefab).GetComponent<Bubble>();
}
}
}