准备对粒子效果等生成性效果进行优化
This commit is contained in:
48
Assets/Scripts/Framework/GameContent/GameObjectPool.cs
Normal file
48
Assets/Scripts/Framework/GameContent/GameObjectPool.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 94ffe9340adcc294c9d67b9f0bb825eb
|
||||||
Reference in New Issue
Block a user