78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
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> >();
|
|
|
|
private Dictionary<string, int> _loadedRecord = new Dictionary<string, int>();
|
|
|
|
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();
|
|
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))
|
|
{
|
|
//DebugUtil.LogError("ReleaseBehavior failed, can not find behavior: {0}", behaviorName);
|
|
return;
|
|
}
|
|
|
|
behaviorPool.Release(behavior);
|
|
}
|
|
}
|
|
} |