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

78 lines
2.5 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using Cysharp.Threading.Tasks;
using Framework;
using UnityEngine;
namespace Gameplay.BT
{
public partial class BTImplement
{
private Dictionary<string, ObjectPool<ExternalBehavior>> _behaviorPoolDic =
new Dictionary<string, ObjectPool<ExternalBehavior> >();
2024-03-05 15:20:45 +08:00
private Dictionary<string, int> _loadedRecord = new Dictionary<string, int>();
2023-08-22 19:08:45 +08:00
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);
}
}
public void ClearPool()
{
foreach (var behaviorPool in _behaviorPoolDic.Values)
{
behaviorPool.Destroy();
}
_behaviorPoolDic.Clear();
2024-03-05 15:20:45 +08:00
UnloadAllRes();
2023-08-22 19:08:45 +08:00
}
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)
{
2024-07-16 12:58:05 +08:00
//只有一个通用行为树
if (!_behaviorPoolDic.TryGetValue(behaviorName, out var behaviorPool))
2023-08-22 19:08:45 +08:00
{
2024-07-16 12:58:05 +08:00
//DebugUtil.LogError("ReleaseBehavior failed, can not find behavior: {0}", behaviorName);
2023-08-22 19:08:45 +08:00
return;
}
behaviorPool.Release(behavior);
}
}
}