using System.Collections.Generic; using BehaviorDesigner.Runtime; using Cysharp.Threading.Tasks; using Framework; using UnityEngine; namespace Gameplay.BT { public partial class BTImplement { private Dictionary> _behaviorPoolDic = new Dictionary >(); private Dictionary _loadedRecord = new Dictionary(); public async UniTask PreLoadAI(HashSet aiNames, int initCount) { foreach (var behaviorName in aiNames) { if (_behaviorPoolDic.ContainsKey(behaviorName)) { continue; } var entry = await LoadBehaviorAsset(behaviorName); var behaviorPool = new ObjectPool( 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 GetBehavior(string behaviorName) { ObjectPool behaviorPool; if (!_behaviorPoolDic.ContainsKey(behaviorName)) { var entry = await LoadBehaviorAsset(behaviorName); if (!_behaviorPoolDic.ContainsKey(behaviorName)) { behaviorPool = new ObjectPool( entry.InstanceExternalBehavior); _behaviorPoolDic.Add(behaviorName, behaviorPool); } } behaviorPool = _behaviorPoolDic[behaviorName]; var behavior = behaviorPool.Get(); return behavior; } private void ReleaseBehavior(string behaviorName, ExternalBehavior behavior) { if (!_behaviorPoolDic.ContainsKey(behaviorName)) { DebugUtil.LogError("ReleaseBehavior failed, can not find behavior: {0}", behaviorName); return; } var behaviorPool = _behaviorPoolDic[behaviorName]; behaviorPool.Release(behavior); } } }