com.alicizax.unity.framework/Runtime/ABase/ObjectPool/IObjectPoolService.cs

81 lines
3.2 KiB
C#
Raw Permalink Normal View History

using System;
2025-10-11 15:18:09 +08:00
using System.Collections.Generic;
using AlicizaX;
namespace AlicizaX.ObjectPool
{
public readonly struct ObjectPoolCreateOptions
{
public readonly string Name;
public readonly bool AllowMultiSpawn;
public readonly float AutoReleaseInterval;
public readonly int Capacity;
public readonly float ExpireTime;
public readonly int Priority;
public ObjectPoolCreateOptions(
string name = "",
bool allowMultiSpawn = false,
float autoReleaseInterval = float.MaxValue,
int capacity = int.MaxValue,
float expireTime = float.MaxValue,
int priority = 0)
{
Name = name ?? string.Empty;
AllowMultiSpawn = allowMultiSpawn;
AutoReleaseInterval = autoReleaseInterval;
Capacity = capacity;
ExpireTime = expireTime;
Priority = priority;
}
public ObjectPoolCreateOptions WithName(string name)
=> new ObjectPoolCreateOptions(name, AllowMultiSpawn, AutoReleaseInterval, Capacity, ExpireTime, Priority);
public static ObjectPoolCreateOptions Single(string name = "")
=> new ObjectPoolCreateOptions(name: name);
public static ObjectPoolCreateOptions Multi(string name = "")
=> new ObjectPoolCreateOptions(name: name, allowMultiSpawn: true);
}
2025-10-11 15:18:09 +08:00
/// <summary>
/// 对象池管理器。
/// </summary>
2026-04-20 13:46:44 +08:00
public interface IObjectPoolService : IService
2025-10-11 15:18:09 +08:00
{
int Count { get; }
2025-10-11 15:18:09 +08:00
bool HasObjectPool<T>() where T : ObjectBase;
bool HasObjectPool(Type objectType);
bool HasObjectPool<T>(string name) where T : ObjectBase;
bool HasObjectPool(Type objectType, string name);
bool HasObjectPool(Predicate<ObjectPoolBase> condition);
IObjectPool<T> GetObjectPool<T>() where T : ObjectBase;
ObjectPoolBase GetObjectPool(Type objectType);
IObjectPool<T> GetObjectPool<T>(string name) where T : ObjectBase;
ObjectPoolBase GetObjectPool(Type objectType, string name);
ObjectPoolBase GetObjectPool(Predicate<ObjectPoolBase> condition);
ObjectPoolBase[] GetObjectPools(Predicate<ObjectPoolBase> condition);
void GetObjectPools(Predicate<ObjectPoolBase> condition, List<ObjectPoolBase> results);
ObjectPoolBase[] GetAllObjectPools();
void GetAllObjectPools(List<ObjectPoolBase> results);
ObjectPoolBase[] GetAllObjectPools(bool sort);
void GetAllObjectPools(bool sort, List<ObjectPoolBase> results);
IObjectPool<T> CreatePool<T>(ObjectPoolCreateOptions options = default) where T : ObjectBase;
ObjectPoolBase CreatePool(Type objectType, ObjectPoolCreateOptions options = default);
2025-10-11 15:18:09 +08:00
bool DestroyObjectPool<T>() where T : ObjectBase;
bool DestroyObjectPool(Type objectType);
bool DestroyObjectPool<T>(string name) where T : ObjectBase;
bool DestroyObjectPool(Type objectType, string name);
bool DestroyObjectPool<T>(IObjectPool<T> objectPool) where T : ObjectBase;
bool DestroyObjectPool(ObjectPoolBase objectPool);
void Release();
void ReleaseAllUnused();
}
}