com.alicizax.unity.framework/Runtime/ABase/GameObjectPool/Data/PoolConfig.cs

134 lines
3.7 KiB
C#
Raw Normal View History

2026-03-26 10:49:41 +08:00
using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace AlicizaX
{
public enum PoolResourceLoaderType
{
AssetBundle = 0,
Resources = 1
}
public enum PoolMatchMode
{
Exact = 0,
Prefix = 1
}
/// <summary>
/// 对象池配置项。
/// </summary>
[Serializable]
public sealed class PoolConfig
{
public const string DefaultGroup = "Default";
public const int DefaultCapacity = 8;
public const float DefaultInstanceIdleTimeout = 30f;
public const float DefaultPrefabUnloadDelay = 60f;
public string group = DefaultGroup;
[FormerlySerializedAs("asset")]
public string assetPath;
public PoolMatchMode matchMode = PoolMatchMode.Exact;
public PoolResourceLoaderType resourceLoaderType = PoolResourceLoaderType.AssetBundle;
[FormerlySerializedAs("time")]
[Min(0f)]
public float instanceIdleTimeout = DefaultInstanceIdleTimeout;
[Min(0f)]
public float prefabUnloadDelay = DefaultPrefabUnloadDelay;
[FormerlySerializedAs("poolCount")]
[Min(1)]
public int capacity = DefaultCapacity;
[Min(0)]
public int prewarmCount;
public bool preloadOnInitialize;
public void Normalize()
{
group = string.IsNullOrWhiteSpace(group) ? DefaultGroup : group.Trim();
assetPath = NormalizeAssetPath(assetPath);
capacity = Mathf.Max(1, capacity);
prewarmCount = Mathf.Clamp(prewarmCount, 0, capacity);
instanceIdleTimeout = Mathf.Max(0f, instanceIdleTimeout);
prefabUnloadDelay = Mathf.Max(0f, prefabUnloadDelay);
}
public bool Matches(string requestedAssetPath, string requestedGroup = null)
{
if (string.IsNullOrWhiteSpace(assetPath) || string.IsNullOrWhiteSpace(requestedAssetPath))
{
return false;
}
if (!string.IsNullOrWhiteSpace(requestedGroup) &&
!string.Equals(group, requestedGroup, StringComparison.Ordinal))
{
return false;
}
return matchMode switch
{
PoolMatchMode.Exact => string.Equals(requestedAssetPath, assetPath, StringComparison.Ordinal),
PoolMatchMode.Prefix => requestedAssetPath.StartsWith(assetPath, StringComparison.Ordinal),
_ => false
};
}
public string BuildResolvedPoolKey(string resolvedAssetPath)
{
return $"{group}|{(int)matchMode}|{assetPath}|{(int)resourceLoaderType}|{resolvedAssetPath}";
}
public static int CompareByPriority(PoolConfig left, PoolConfig right)
{
if (ReferenceEquals(left, right))
{
return 0;
}
if (left == null)
{
return 1;
}
if (right == null)
{
return -1;
}
int modeCompare = left.matchMode.CompareTo(right.matchMode);
if (modeCompare != 0)
{
return modeCompare;
}
int pathLengthCompare = right.assetPath.Length.CompareTo(left.assetPath.Length);
if (pathLengthCompare != 0)
{
return pathLengthCompare;
}
return string.Compare(left.group, right.group, StringComparison.Ordinal);
}
public static string NormalizeAssetPath(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
return value.Trim().Replace('\\', '/');
}
}
}