T Create<T>() where T : IndicatorComponent
{
Type type = typeof(T);
T obj = Resources.Load<T>(Path.Indicators + type.ToString());
T inst = Instantiate(obj);
if(!indicatorPool.ContainsKey(type))
indicatorPool.Add(type, new List<IndicatorComponent>());
indicatorPool[type].Add(inst);
return inst;
}
bool TryGet<T>(out T result) where T : IndicatorComponent
{
result = null;
Type type = typeof(T);
if (!indicatorPool.ContainsKey(type))
return false;
foreach (var comp in indicatorPool[type])
{
if (comp.IsUnique || !comp.gameObject.activeInHierarchy)
{
result = comp as T;
return true;
}
}
return false;
}
public T Get<T>() where T : IndicatorComponent
{
if (TryGet<T>(out T indicator))
{
return indicator;
}
else
{
T instance = Create<T>();
return instance;
}
}
public void Show<T>(Vector2 coord) where T : IndicatorComponent
{
Get<T>().Show(coord);
}
이렇게 하면 본래 각각의 인디케이터마다 Show, Get, Create 하던 것을 단 3개로 줄일 수 있다.
public void Show<T>(Vector2 coord) where T : IndicatorComponent
{
Get<T>().Show(coord);
}
#내일배움캠프 #스파르타내일배움캠프 #스파르타내일배움캠프TIL