99 lines
2.6 KiB
C#
99 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Pool;
|
|
using WI;
|
|
using XED.Interfaces;
|
|
|
|
namespace XED.Manage
|
|
{
|
|
//TODO::전체 수정 필요. 개별적인 ObjectPool이 존재하고 이 매니저는 그에 대한 접근 API만 제공하면 됨.
|
|
public class ObjectPoolManager : MonoBehaviour, ISingle
|
|
{
|
|
[Serializable]
|
|
public class ObjectInfo
|
|
{
|
|
//public Type objectType;
|
|
public GameObject prefab;
|
|
public int count;
|
|
|
|
public ObjectInfo(GameObject item, int count)
|
|
{
|
|
this.prefab = item;
|
|
this.count = count;
|
|
}
|
|
}
|
|
|
|
public List<ObjectInfo> objectInfos = new();
|
|
|
|
HashSet<Type> cachingTypes = new();
|
|
|
|
Dictionary<Type, ObjectPool<Component>> cachingPools = new();
|
|
Dictionary<Type, GameObject> prefabTable = new();
|
|
|
|
public void AddObjectInfo(GameObject pool)
|
|
{
|
|
var item = new ObjectInfo(pool, 3);
|
|
pool.gameObject.SetActive(false);
|
|
objectInfos.Add(item);
|
|
}
|
|
public T Get<T>() where T : Component
|
|
{
|
|
ObjectInfo targetInfo = null;
|
|
if (!cachingTypes.Contains(typeof(T)))
|
|
{
|
|
foreach(var info in objectInfos)
|
|
{
|
|
if (!info.prefab.TryGetComponent(out T comp))
|
|
continue;
|
|
|
|
prefabTable.Add(typeof(T), info.prefab);
|
|
cachingTypes.Add(typeof(T));
|
|
|
|
var newPool = new ObjectPool<Component>
|
|
(Create,
|
|
Get,
|
|
Release,
|
|
Destroy, true, info.count);
|
|
|
|
Component Create()
|
|
{
|
|
var result = Instantiate(comp);
|
|
result.GetComponent<IPooledObject>().Pool = cachingPools[typeof(T)];
|
|
return result.GetComponent<T>();
|
|
}
|
|
cachingPools.Add(typeof(T), newPool);
|
|
targetInfo = info;
|
|
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!cachingTypes.Contains(typeof(T)))
|
|
{
|
|
Debug.LogError($"{typeof(T)} is NOT POOL");
|
|
return default;
|
|
}
|
|
|
|
return cachingPools[typeof(T)].Get().GetComponent<T>();
|
|
}
|
|
|
|
|
|
void Get(Component comp)
|
|
{
|
|
comp.gameObject.SetActive(true);
|
|
}
|
|
|
|
void Release(Component comp)
|
|
{
|
|
//comp.gameObject.SetActive(false);
|
|
}
|
|
|
|
void Destroy(Component comp)
|
|
{
|
|
}
|
|
}
|
|
|
|
}
|