IndieGame/client/Assets/Ether/Scripts/Module/Pool/ObjectPool.cs

64 lines
1.5 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: 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<object> objectQueue = new Queue<object>();
public void Push(object obj)
{
objectQueue.Enqueue(obj);
}
public T Get<T>() 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;
}
}
}