NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/BTImplement.Pool.cs

72 lines
2.2 KiB
C#

using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using Cysharp.Threading.Tasks;
using Framework;
namespace Gameplay.BT
{
public partial class BTImplement
{
private Dictionary<string, ObjectPool<ExternalBehavior>> _behaviorPoolDic = new ();
private Dictionary<string, int> _loadedRecord = new ();
public async UniTask PreLoadAI(HashSet<string> aiNames, int initCount)
{
foreach (var behaviorName in aiNames)
{
if (_behaviorPoolDic.ContainsKey(behaviorName))
{
continue;
}
var entry = await LoadBehaviorAsset(behaviorName);
var behaviorPool = new ObjectPool<ExternalBehavior>(
entry.InstanceExternalBehavior, null, null,
initCount);
_behaviorPoolDic.Add(behaviorName, behaviorPool);
}
}
private void ClearPool()
{
foreach (var behaviorPool in _behaviorPoolDic.Values)
{
behaviorPool.Destroy();
}
_behaviorPoolDic.Clear();
UnloadAllRes();
}
private async UniTask<ExternalBehavior> GetBehavior(string behaviorName)
{
ObjectPool<ExternalBehavior> behaviorPool;
if (!_behaviorPoolDic.ContainsKey(behaviorName))
{
var entry = await LoadBehaviorAsset(behaviorName);
if (!_behaviorPoolDic.ContainsKey(behaviorName))
{
behaviorPool = new ObjectPool<ExternalBehavior>(
entry.InstanceExternalBehavior);
_behaviorPoolDic.Add(behaviorName, behaviorPool);
}
}
behaviorPool = _behaviorPoolDic[behaviorName];
var behavior = behaviorPool.Get();
return behavior;
}
private void ReleaseBehavior(string behaviorName, ExternalBehavior behavior)
{
if (!_behaviorPoolDic.TryGetValue(behaviorName, out var behaviorPool))
return;
behaviorPool.Release(behavior);
}
}
}