49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
|
|
using Convention;
|
||
|
|
using Convention.Experimental.Modules;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using static Convention.Experimental.Architecture;
|
||
|
|
|
||
|
|
namespace Demo.Game
|
||
|
|
{
|
||
|
|
public class GameObjectPool : MonoSingleton<GameObjectPool>
|
||
|
|
{
|
||
|
|
private Dictionary<Func<ScriptableObject>, ScriptableObject> Pool = new();
|
||
|
|
|
||
|
|
public ScriptableObject Spawn(Func<ScriptableObject> creater)
|
||
|
|
{
|
||
|
|
if (Pool.TryGetValue(creater, out var prefab) == false)
|
||
|
|
{
|
||
|
|
prefab = Pool[creater] = creater();
|
||
|
|
prefab.gameObject.SetActive(false);
|
||
|
|
prefab.transform.SetParent(transform);
|
||
|
|
}
|
||
|
|
if (prefab is CameraObject)
|
||
|
|
{
|
||
|
|
return prefab;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var result = GetModule<GameObjectPoolManager>().Spawn(prefab.gameObject).GetComponent<ScriptableObject>();
|
||
|
|
result.gameObject.SetActive(true);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Unspawn(ScriptableObject obj)
|
||
|
|
{
|
||
|
|
if (obj is CameraObject camera)
|
||
|
|
{
|
||
|
|
camera.gameObject.SetActive(false);
|
||
|
|
camera.transform.SetParent(transform);
|
||
|
|
}
|
||
|
|
else if (obj is RootObject)
|
||
|
|
return;
|
||
|
|
else
|
||
|
|
{
|
||
|
|
GetModule<GameObjectPoolManager>().Unspawn(obj.gameObject);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|