提交
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Roy.ObjectPool
|
||||
{
|
||||
|
||||
public class ObjectPool<T> where T : ObjectPoolItem
|
||||
{
|
||||
private readonly Queue<T> _pool; // 存储可重用对象的队列
|
||||
private readonly T _prefab; // 预制体
|
||||
private readonly string _poolName; // 预制体
|
||||
private readonly Transform _parent; // 对象的父级
|
||||
|
||||
// 构造函数
|
||||
public ObjectPool(string objectName, T prefab, int initialSize, Transform parent = null)
|
||||
{
|
||||
_poolName = objectName;
|
||||
_prefab = prefab;
|
||||
_parent = parent;
|
||||
_pool = new Queue<T>();
|
||||
|
||||
// 初始化对象池
|
||||
for (int i = 0; i < initialSize; i++)
|
||||
{
|
||||
T obj = CreateInstance();
|
||||
_pool.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建对象的实例
|
||||
private T CreateInstance()
|
||||
{
|
||||
var gameObject = Object.Instantiate(_prefab.gameObject, _parent);
|
||||
gameObject.SetActive(false); // 初始化时设置为不激活
|
||||
var instance = gameObject.GetComponent<T>();
|
||||
instance.objectName = _poolName;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// 获取一个对象
|
||||
public T Get()
|
||||
{
|
||||
if (_pool.Count > 0)
|
||||
{
|
||||
T obj = _pool.Dequeue();
|
||||
obj.gameObject.SetActive(true); // 获取时激活对象
|
||||
return obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有可用对象,创建一个新的
|
||||
var obj = CreateInstance();
|
||||
obj.gameObject.SetActive(true);// 激活对象
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
// 归还对象
|
||||
public void ReturnToPool(T obj)
|
||||
{
|
||||
obj.OnRecycle();
|
||||
|
||||
obj.gameObject.SetActive(false); // 归还时设置为不激活
|
||||
obj.transform.SetParent(_parent);
|
||||
obj.transform.localPosition = Vector3.zero;
|
||||
|
||||
|
||||
_pool.Enqueue(obj); // 将对象返回池中
|
||||
}
|
||||
|
||||
// 清空对象池
|
||||
public void ClearPool()
|
||||
{
|
||||
while (_pool.Count > 0)
|
||||
{
|
||||
T obj = _pool.Dequeue();
|
||||
Object.Destroy(obj.gameObject); // 清除所有对象
|
||||
}
|
||||
|
||||
Object.Destroy(_parent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43b0aa3ccb0740cdacd10e06164ecb74
|
||||
timeCreated: 1728964545
|
||||
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Roy.ObjectPool
|
||||
{
|
||||
public class ObjectPoolItem : MonoBehaviour
|
||||
{
|
||||
public string objectName;
|
||||
|
||||
/// <summary>
|
||||
/// 返回对象池,回收时执行
|
||||
/// </summary>
|
||||
public virtual void OnRecycle()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97032cf20b09454ca983b038b9ff147b
|
||||
timeCreated: 1729045887
|
||||
@@ -0,0 +1,117 @@
|
||||
using System.Collections.Generic;
|
||||
using ScrewsMaster;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Roy.ObjectPool
|
||||
{
|
||||
|
||||
public class PoolManager : SingletonMonoBehaviour<PoolManager>
|
||||
{
|
||||
private GameObject _gameObjectParentNode;
|
||||
private GameObject _uiPoolParentNode;
|
||||
private readonly Dictionary<string, object> _pools = new Dictionary<string, object>();
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
_gameObjectParentNode = new GameObject("GameObjectPool");
|
||||
_gameObjectParentNode.transform.SetParent(transform); // 设置为对象池管理类的子节点
|
||||
|
||||
// 创建一个用于存放UI元素的节点
|
||||
_uiPoolParentNode = new GameObject("UIPool");
|
||||
_uiPoolParentNode.transform.SetParent(transform); // 设置为对象池管理类的子节点
|
||||
|
||||
// 添加必需的组件
|
||||
var canvas = _uiPoolParentNode.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceCamera;
|
||||
foreach (var c in Camera.allCameras)
|
||||
{
|
||||
if (!c.name.Equals("Camera")) continue;//TODO 当前是去找固定名称的摄像机 后续看看怎么优化调整
|
||||
canvas.worldCamera = c;
|
||||
break;
|
||||
}
|
||||
|
||||
var canvasScaler = _uiPoolParentNode.AddComponent<CanvasScaler>(); // 添加适配组件
|
||||
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
||||
canvasScaler.referenceResolution = new Vector2(Screen.width, Screen.height);
|
||||
_uiPoolParentNode.AddComponent<GraphicRaycaster>(); // 添加事件处理组件
|
||||
|
||||
base.Awake();
|
||||
|
||||
}
|
||||
|
||||
// 创建对象池
|
||||
public void CreatePool<T>(string poolName, T prefab, int initialSize, bool isUI = false) where T : ObjectPoolItem
|
||||
{
|
||||
if (!_pools.ContainsKey(poolName))
|
||||
{
|
||||
var poolGameObject = new GameObject(poolName);
|
||||
if (isUI)
|
||||
{
|
||||
poolGameObject.AddComponent<RectTransform>();
|
||||
poolGameObject.SetParent(_uiPoolParentNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
poolGameObject.SetParent(_gameObjectParentNode);
|
||||
}
|
||||
|
||||
ObjectPool<T> pool = new ObjectPool<T>(poolName, prefab, initialSize, poolGameObject.transform);
|
||||
_pools.Add(poolName, pool);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"尝试创建已经存在的的对象池,请检查 !!!");
|
||||
}
|
||||
}
|
||||
|
||||
// 获取对象
|
||||
public T GetObject<T>(string poolName) where T : ObjectPoolItem
|
||||
{
|
||||
if (_pools.TryGetValue(poolName, out var pool))
|
||||
{
|
||||
return ((ObjectPool<T>)pool).Get();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"尝试获取没有初始化的对象池,请检查!!!!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否存在名称为 poolName 的对象池
|
||||
public bool HasPool(string poolName)
|
||||
{
|
||||
return _pools.ContainsKey(poolName);
|
||||
}
|
||||
|
||||
// 归还对象
|
||||
public void ReturnObject<T>(string poolName, T obj) where T : ObjectPoolItem
|
||||
{
|
||||
if (_pools.TryGetValue(poolName, out var pool))
|
||||
{
|
||||
((ObjectPool<T>)pool).ReturnToPool(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(obj.gameObject);
|
||||
Debug.LogError($"归还了不存在对象池 {poolName} 的对象");
|
||||
}
|
||||
}
|
||||
|
||||
// 清空指定池
|
||||
public void ClearPool(string poolName)
|
||||
{
|
||||
if (_pools.TryGetValue(poolName, out var pool))
|
||||
{
|
||||
((ObjectPool<ObjectPoolItem>)pool).ClearPool();
|
||||
_pools.Remove(poolName); // 清空后移除池
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"尝试清空不存在的对象池,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bfc0527198b24e8983adfd0c2f9e2ef1
|
||||
timeCreated: 1728964617
|
||||
Reference in New Issue
Block a user