/******************************************************************** 文件: ObjectPool.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 17:01:47 功能: 类对象池 *********************************************************************/ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Ether { public class ObjectPool { private Queue objectQueue = new Queue(); public void Push(object obj) { objectQueue.Enqueue(obj); } public T Get() where T : new() { T obj; if (objectQueue.Count > 0) { obj = (T)objectQueue.Dequeue(); } else { obj = new T(); } return obj; } public object Get(string typeName) { Type type = Type.GetType(typeName); return Get(type); } public object Get(Type type) { if (type != null && type.IsSubclassOf(type) && type.IsClass && !type.IsAbstract) { return Activator.CreateInstance(type); } else { throw new ArgumentException("Type name is not valid or not suitable for creation: " + type); } } public int GetCount() { return objectQueue.Count; } } }