61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
/*
|
|
* @Author: 白哉
|
|
* @Description:
|
|
* @Date: 2025年08月07日 星期四 15:08:10
|
|
* @Modify:
|
|
*/
|
|
|
|
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Logic.PrefabPool
|
|
{
|
|
public class PrefabPoolManager
|
|
{
|
|
public static PrefabPoolManager Instance = new PrefabPoolManager();
|
|
private PrefabConfig _config;
|
|
private Dictionary<PrefabType, List<GameObject>> _prefabPool = new Dictionary<PrefabType, List<GameObject>>();
|
|
|
|
public void Init()
|
|
{
|
|
if (_config != null) return;
|
|
_config = Resources.Load<PrefabConfig>("DataAssets/PrefabConfig");
|
|
}
|
|
|
|
private GameObject GetPrefab(PrefabType type)
|
|
{
|
|
var item = _config.GetPrefab(type);
|
|
if (item == null) return null;
|
|
return item.Prefab;
|
|
}
|
|
|
|
// 获取实例
|
|
public GameObject GetGameObject(PrefabType type)
|
|
{
|
|
if (_prefabPool.ContainsKey(type) && _prefabPool[type].Count > 0)
|
|
{
|
|
var obj = _prefabPool[type][0];
|
|
_prefabPool[type].RemoveAt(0);
|
|
return obj;
|
|
}
|
|
|
|
var prefab = GetPrefab(type);
|
|
if (prefab == null) return null;
|
|
return GameObject.Instantiate(prefab);
|
|
}
|
|
|
|
// 返回实例
|
|
public void ReturnGameObject(PrefabType type, GameObject obj)
|
|
{
|
|
if (obj == null) return;
|
|
if (!_prefabPool.ContainsKey(type)) _prefabPool[type] = new List<GameObject>();
|
|
if (_prefabPool[type].Count > 30)
|
|
{
|
|
GameObject.Destroy(obj);
|
|
return;
|
|
}
|
|
_prefabPool[type].Add(obj);
|
|
}
|
|
}
|
|
} |