首次提交
This commit is contained in:
@@ -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($"尝试清空不存在的对象池,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user