GObjectPool.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using CombatLibrary.CombatLibrary.CombatCore.Utility;
  5. using Fort23.Core;
  6. #if !COMBAT_SERVER
  7. using Core.Pool.GPool;
  8. using UnityEngine;
  9. using Object = UnityEngine.Object;
  10. #endif
  11. namespace Fort23.UTool
  12. {
  13. public class GameObjectQueue
  14. {
  15. public string poolName
  16. {
  17. get { return _poolName; }
  18. }
  19. private bool _isDis;
  20. private string _poolName;
  21. private string _prefabName;
  22. private int maxCount = -1;
  23. #if !COMBAT_SERVER
  24. /// <summary>
  25. /// 池子的父亲
  26. /// </summary>
  27. private AssetHandle _PoolObjectFather;
  28. public bool isOne;
  29. private readonly List<IGObjectPoolInterface> _usePool = new List<IGObjectPoolInterface>();
  30. private readonly List<IGObjectPoolInterface> _queue = new List<IGObjectPoolInterface>();
  31. private readonly List<IGObjectPoolInterface> _destroyPool = new List<IGObjectPoolInterface>();
  32. private readonly List<IGObjectPoolInterface> _delayHide = new List<IGObjectPoolInterface>();
  33. public int initCopunt;
  34. private bool _isSetMaxCount;
  35. public void Init(string poolName, string prefabName)
  36. {
  37. _poolName = poolName;
  38. _prefabName = prefabName;
  39. }
  40. public async CTask Enqueue(IGObjectPoolInterface poolInterface, bool isNotAwaitDelay = false)
  41. {
  42. if (poolInterface == null || poolInterface.own == null)
  43. {
  44. return;
  45. }
  46. if (_queue.Contains(poolInterface))
  47. {
  48. return;
  49. }
  50. if (_delayHide.Contains(poolInterface))
  51. {
  52. return;
  53. }
  54. _delayHide.Add(poolInterface);
  55. if (!isNotAwaitDelay)
  56. {
  57. await poolInterface.DelayHide();
  58. }
  59. if (_isDis)
  60. {
  61. return;
  62. }
  63. _delayHide.Remove(poolInterface);
  64. poolInterface.isUse = false;
  65. _usePool.Remove(poolInterface);
  66. if (poolInterface.own != null)
  67. {
  68. poolInterface.DormancyObj();
  69. this._queue.Add(poolInterface);
  70. }
  71. }
  72. public void AddDestroy(IGObjectPoolInterface poolInterface, bool immediately = false)
  73. {
  74. if (poolInterface.own == null)
  75. {
  76. return;
  77. }
  78. if (!_destroyPool.Contains(poolInterface))
  79. {
  80. if (immediately)
  81. {
  82. poolInterface.DestroyTimer = null;
  83. GameObjectDestroyPool.Instance.DestroyObject(poolInterface.own);
  84. }
  85. else
  86. {
  87. TimerComponent.Instance.Remove(poolInterface.DestroyTimer);
  88. poolInterface.DestroyTimer =
  89. TimerComponent.Instance.AddTimer(6000, delegate
  90. {
  91. _destroyPool.Remove(poolInterface);
  92. poolInterface.DestroyTimer = null;
  93. GameObjectDestroyPool.Instance.DestroyObject(poolInterface.own);
  94. });
  95. _destroyPool.Add(poolInterface);
  96. }
  97. }
  98. }
  99. public void DormancyPool()
  100. {
  101. List<IGObjectPoolInterface> alliG = new List<IGObjectPoolInterface>();
  102. IGObjectPoolInterface[] all = _usePool.ToArray();
  103. if (all != null)
  104. {
  105. alliG.AddRange(all);
  106. }
  107. for (int i = 0; i < alliG.Count; i++)
  108. {
  109. IGObjectPoolInterface poolInterface = alliG[i];
  110. Enqueue(poolInterface);
  111. }
  112. // _usePool.Clear();
  113. }
  114. public async CTask Preset<T>(Clock clock, bool isUseSynchronous) where T : IGObjectPoolInterface
  115. {
  116. DormancyPool();
  117. for (int i = 0; i < Count; i++)
  118. {
  119. IGObjectPoolInterface poolInterface = _queue[i];
  120. DisposePoolEntity(poolInterface);
  121. poolInterface.Preset();
  122. }
  123. for (int i = 0; i < _usePool.Count; i++)
  124. {
  125. IGObjectPoolInterface poolInterface = _usePool[i];
  126. DisposePoolEntity(poolInterface);
  127. poolInterface.Preset();
  128. }
  129. for (int i = 0; i < _destroyPool.Count; i++)
  130. {
  131. DisposePoolEntity(_destroyPool[i]);
  132. }
  133. _destroyPool.Clear();
  134. T OBJ = await Dequeue<T>(true, clock, isUseSynchronous);
  135. Enqueue(OBJ, true);
  136. }
  137. private async CTask<AssetHandle> LoadAsset(Clock clock, bool isUseSynchronous)
  138. {
  139. if (AssetBundleLoadManager.Instance != null)
  140. {
  141. CTask<AssetHandle> cTask =
  142. AssetBundleLoadManager.Instance.LoadAssetAsyncTask<GameObject>(_prefabName, clock: clock,
  143. isUseSynchronous: isUseSynchronous);
  144. AssetHandle assetHandle = await cTask;
  145. return assetHandle;
  146. }
  147. return null;
  148. }
  149. public T DequeuForSynchronize<T>(GameObject gameObject, bool isActive) where T : IGObjectPoolInterface
  150. {
  151. if (_queue.Count <= 0)
  152. {
  153. return CreateInstance<T>(isActive, gameObject);
  154. }
  155. if (_destroyPool.Count > 0)
  156. {
  157. for (int i = 0; i < _destroyPool.Count; i++)
  158. {
  159. DisposePoolEntity(_destroyPool[i]);
  160. }
  161. _destroyPool.Clear();
  162. }
  163. for (int i = 0; i < _queue.Count; i++)
  164. {
  165. if (_queue[i].own == null)
  166. {
  167. _queue.RemoveAt(i);
  168. i--;
  169. continue;
  170. }
  171. else
  172. {
  173. IGObjectPoolInterface objectPoolInterface = this._queue[0];
  174. _queue.RemoveAt(0);
  175. UseObject(objectPoolInterface, isActive);
  176. return (T)objectPoolInterface;
  177. }
  178. }
  179. return CreateInstance<T>(isActive, gameObject);
  180. }
  181. public async CTask<T> Dequeue<T>(bool isActive, Clock clock, bool isUseSynchronous)
  182. where T : IGObjectPoolInterface
  183. {
  184. if (maxCount > 0 && _usePool.Count > maxCount)
  185. {
  186. return default;
  187. }
  188. for (int i = 0; i < _queue.Count; i++)
  189. {
  190. if (_queue[i].own == null)
  191. {
  192. _queue.RemoveAt(i);
  193. i--;
  194. continue;
  195. }
  196. else
  197. {
  198. IGObjectPoolInterface objectPoolInterface = this._queue[i];
  199. if (objectPoolInterface.own == null)
  200. {
  201. continue;
  202. }
  203. _queue.RemoveAt(i);
  204. UseObject(objectPoolInterface, isActive);
  205. return (T)objectPoolInterface;
  206. }
  207. }
  208. bool isUsePoolObject = false;
  209. if ((_PoolObjectFather == null || _PoolObjectFather.IsNull()) && !string.IsNullOrEmpty(_prefabName))
  210. {
  211. using (await CoroutineLockComponent.Instance.Wait(poolName, 20000))
  212. {
  213. if (_PoolObjectFather == null)
  214. {
  215. _PoolObjectFather = await LoadAsset(clock, isUseSynchronous);
  216. isUsePoolObject = true;
  217. }
  218. }
  219. }
  220. // if (clock != null && clock.isBreak)
  221. // {
  222. // if (isUsePoolObject)
  223. // {
  224. // Debug.LogError("已打断,但是有初始化数据" + _PoolObjectFather);
  225. // }
  226. // // return default;
  227. // }
  228. if (_queue.Count <= 0)
  229. {
  230. AssetHandle assetHandle = _PoolObjectFather;
  231. if (!isUsePoolObject)
  232. {
  233. assetHandle = await _PoolObjectFather.InstantiateSync(isUseSynchronous);
  234. }
  235. // AssetHandle assetHandle = isUsePoolObject ? _PoolObjectFather : _PoolObjectFather;
  236. return CreateInstance<T>(isActive, assetHandle);
  237. }
  238. if (_destroyPool.Count > 0)
  239. {
  240. for (int i = 0; i < _destroyPool.Count; i++)
  241. {
  242. DisposePoolEntity(_destroyPool[i]);
  243. }
  244. _destroyPool.Clear();
  245. }
  246. AssetHandle assetHandle2 = _PoolObjectFather;
  247. if (!isUsePoolObject)
  248. {
  249. assetHandle2 = await _PoolObjectFather.InstantiateSync(isUseSynchronous);
  250. }
  251. assetHandle2 = await assetHandle2.InstantiateSync(isUseSynchronous);
  252. return CreateInstance<T>(isActive, assetHandle2);
  253. }
  254. private T CreateInstance<T>(bool isActive, AssetHandle assetHandle) where T : IGObjectPoolInterface
  255. {
  256. if (assetHandle == null)
  257. {
  258. return default;
  259. }
  260. T poolInerface = Activator.CreateInstance<T>();
  261. GameObject go = assetHandle.AssetObject<GameObject>();
  262. GameObjectEntity objectEntity = go.GetComponent<GameObjectEntity>();
  263. if (objectEntity == null)
  264. {
  265. objectEntity = go.AddComponent<GameObjectEntity>();
  266. }
  267. objectEntity.IgObjectPoolInterface = poolInerface;
  268. objectEntity.OnDestroyCallBack = DestroyEntity;
  269. poolInerface.SetGameObject(go);
  270. poolInerface.poolObjName = _poolName;
  271. poolInerface.AssetHandle = assetHandle;
  272. UseObject(poolInerface, isActive);
  273. initCopunt++;
  274. return poolInerface;
  275. }
  276. private T CreateInstance<T>(bool isActive, GameObject gameObject) where T : IGObjectPoolInterface
  277. {
  278. if (gameObject == null)
  279. {
  280. return default;
  281. }
  282. T poolInerface = Activator.CreateInstance<T>();
  283. GameObject go = GameObject.Instantiate(gameObject);
  284. GameObjectEntity objectEntity = go.GetComponent<GameObjectEntity>();
  285. if (objectEntity == null)
  286. {
  287. objectEntity = go.AddComponent<GameObjectEntity>();
  288. }
  289. objectEntity.IgObjectPoolInterface = poolInerface;
  290. objectEntity.OnDestroyCallBack = DestroyEntity;
  291. poolInerface.SetGameObject(go);
  292. poolInerface.poolObjName = _poolName;
  293. poolInerface.AssetHandle = null;
  294. UseObject(poolInerface, isActive);
  295. initCopunt++;
  296. return poolInerface;
  297. }
  298. private void DestroyEntity(IGObjectPoolInterface poolInterface)
  299. {
  300. if (poolInterface != null)
  301. {
  302. _queue.Remove(poolInterface);
  303. _usePool.Remove(poolInterface);
  304. }
  305. else
  306. {
  307. return;
  308. }
  309. DisposePoolEntity(poolInterface);
  310. // poolInterface.DormancyObj();
  311. poolInterface.DestroyObj();
  312. poolInterface.AssetHandle?.Release();
  313. if (poolInterface.AssetHandle == _PoolObjectFather)
  314. {
  315. _PoolObjectFather = null;
  316. if (_queue.Count > 0)
  317. {
  318. _PoolObjectFather = _queue[0].AssetHandle;
  319. }
  320. }
  321. if (_queue.Count <= 0 && _usePool.Count <= 0) //池子已经被释放完,把父物体也释掉
  322. {
  323. GObjectPool.Instance.DestroyPool(poolName);
  324. }
  325. }
  326. private void UseObject(IGObjectPoolInterface objectPoolInterface, bool isActive)
  327. {
  328. if (!_isSetMaxCount)
  329. {
  330. _isSetMaxCount = true;
  331. GObjectPoolMaxConfig gObjectPoolMaxConfig =
  332. objectPoolInterface.own.GetComponent<GObjectPoolMaxConfig>();
  333. if (gObjectPoolMaxConfig != null)
  334. {
  335. maxCount = gObjectPoolMaxConfig.maxCount;
  336. }
  337. }
  338. _usePool.Add(objectPoolInterface);
  339. DisposePoolEntity(objectPoolInterface);
  340. // TimerComponent.Instance.Remove(objectPoolInterface.DestroyTimer);
  341. // GameObjectDestroyPool.Instance.RomoveDestroyObject(objectPoolInterface.own);
  342. objectPoolInterface.isUse = true;
  343. objectPoolInterface.ResetData();
  344. if (isActive)
  345. {
  346. objectPoolInterface.ActiveObj();
  347. }
  348. }
  349. public int Count => this._queue.Count;
  350. public void Dispose()
  351. {
  352. _isDis = true;
  353. for (int i = 0; i < Count; i++)
  354. {
  355. IGObjectPoolInterface poolInterface = _queue[i];
  356. DisposePoolEntity(poolInterface);
  357. if (poolInterface.own != null)
  358. {
  359. GameObjectEntity gameObjectEntity = poolInterface.own.GetComponent<GameObjectEntity>();
  360. if (gameObjectEntity != null)
  361. {
  362. gameObjectEntity.IgObjectPoolInterface = null;
  363. gameObjectEntity.OnDestroyCallBack = null;
  364. }
  365. poolInterface.own.SetActive(false);
  366. poolInterface.DormancyObj();
  367. poolInterface.DestroyObj();
  368. poolInterface.AssetHandle?.Release();
  369. GameObjectDestroyPool.Instance.DestroyObject(poolInterface.own);
  370. }
  371. }
  372. for (int i = 0; i < _delayHide.Count; i++)
  373. {
  374. IGObjectPoolInterface poolInterface = _delayHide[i];
  375. DisposePoolEntity(poolInterface);
  376. if (poolInterface.own != null)
  377. {
  378. GameObjectEntity gameObjectEntity = poolInterface.own.GetComponent<GameObjectEntity>();
  379. if (gameObjectEntity != null)
  380. {
  381. gameObjectEntity.IgObjectPoolInterface = null;
  382. gameObjectEntity.OnDestroyCallBack = null;
  383. }
  384. poolInterface.own.SetActive(false);
  385. poolInterface.DormancyObj();
  386. poolInterface.DestroyObj();
  387. poolInterface.AssetHandle?.Release();
  388. GameObjectDestroyPool.Instance.DestroyObject(poolInterface.own);
  389. }
  390. }
  391. for (int i = 0; i < _usePool.Count; i++)
  392. {
  393. IGObjectPoolInterface poolInterface = _usePool[i];
  394. DisposePoolEntity(poolInterface);
  395. if (poolInterface.own != null)
  396. {
  397. GameObjectEntity gameObjectEntity = poolInterface.own.GetComponent<GameObjectEntity>();
  398. if (gameObjectEntity != null)
  399. {
  400. gameObjectEntity.IgObjectPoolInterface = null;
  401. gameObjectEntity.OnDestroyCallBack = null;
  402. }
  403. poolInterface.own.SetActive(false);
  404. poolInterface.DormancyObj();
  405. poolInterface.DestroyObj();
  406. GameObjectDestroyPool.Instance.DestroyObject(poolInterface.own);
  407. }
  408. }
  409. _PoolObjectFather = null;
  410. _delayHide.Clear();
  411. _queue.Clear();
  412. _destroyPool.Clear();
  413. _usePool.Clear();
  414. }
  415. private void DisposePoolEntity(IGObjectPoolInterface poolInterface)
  416. {
  417. _destroyPool.Remove(poolInterface);
  418. TimerComponent.Instance.Remove(poolInterface.DestroyTimer);
  419. GameObjectDestroyPool.Instance.RomoveDestroyObject(poolInterface.own);
  420. poolInterface.DestroyTimer = null;
  421. }
  422. #endif
  423. }
  424. public class GObjectPool
  425. {
  426. private static GObjectPool _instance;
  427. public static GObjectPool Instance
  428. {
  429. get
  430. {
  431. if (_instance == null)
  432. {
  433. _instance = new GObjectPool();
  434. }
  435. return _instance;
  436. }
  437. }
  438. /// <summary>
  439. /// key是gameObject.name.GetHashCode();
  440. /// </summary>
  441. private readonly Dictionary<string, GameObjectQueue> _dictionary = new Dictionary<string, GameObjectQueue>();
  442. /// <summary>
  443. /// 在ab包里面的东西,都可以通过这个方法获取
  444. /// </summary>
  445. /// <param name="prefabName"></param>
  446. /// <param name="callBack"></param>
  447. /// <param name="clock"></param>
  448. /// <param name="Prestore"></param>
  449. /// <param name="poolName"></param>
  450. /// <typeparam name="T"></typeparam>
  451. /// <returns></returns>
  452. public async CTask<T> FetchAsync<T>(string prefabName, System.Action<T> callBack = null, Clock clock = null,
  453. bool Prestore = false, string poolName = null, bool isUseSynchronous = false)
  454. where T : IGObjectPoolInterface
  455. {
  456. if (!prefabName.Contains(".prefab"))
  457. {
  458. prefabName += ".prefab";
  459. }
  460. T poolInerface = await FetchObjectAsync<T>(prefabName, clock, Prestore, poolName,
  461. isUseSynchronous: isUseSynchronous);
  462. if (Prestore)
  463. {
  464. callBack?.Invoke(default);
  465. return default;
  466. }
  467. if (poolInerface == null)
  468. {
  469. callBack?.Invoke(default);
  470. return default;
  471. }
  472. if (clock != null && clock.isBreak)
  473. {
  474. Recycle(poolInerface);
  475. callBack?.Invoke(default);
  476. return default;
  477. }
  478. callBack?.Invoke(poolInerface);
  479. return poolInerface;
  480. }
  481. private string GetPoolName<T>(string name)
  482. {
  483. return name + typeof(T);
  484. }
  485. #if !COMBAT_SERVER
  486. public T FetchAsyncForGameObject<T>(GameObject gameObject, string poolName
  487. ) where T : IGObjectPoolInterface
  488. {
  489. T poolInerface = Fetch<T>(poolName, gameObject);
  490. if (poolInerface == null)
  491. {
  492. return default;
  493. }
  494. return poolInerface;
  495. }
  496. public T Fetch<T>(string poolName, GameObject gameObject, bool isActive = true) where T : IGObjectPoolInterface
  497. {
  498. string newPoolName = GetPoolName<T>(poolName);
  499. GameObjectQueue gameObjectQueue = null;
  500. if (!this._dictionary.TryGetValue(newPoolName, out gameObjectQueue))
  501. {
  502. gameObjectQueue = EnterPool<T>(newPoolName, null);
  503. }
  504. return gameObjectQueue.DequeuForSynchronize<T>(gameObject, isActive);
  505. }
  506. #endif
  507. private async CTask<T> FetchObjectAsync<T>(string prefabName, Clock clock, bool Prestore, string poolName,
  508. bool isActive = true, bool isUseSynchronous = false) where T : IGObjectPoolInterface
  509. {
  510. #if !COMBAT_SERVER
  511. try
  512. {
  513. GameObjectQueue gameObjectQueue = null;
  514. poolName = poolName == null ? prefabName : poolName;
  515. poolName = GetPoolName<T>(poolName);
  516. // 加一个协程锁,让同时加载资源的时候lock住
  517. // using (await CoroutineLockComponent.Instance.Wait(poolName,20000))
  518. {
  519. if (clock != null && clock.isBreak)
  520. {
  521. return default;
  522. }
  523. if (this._dictionary.TryGetValue(poolName, out gameObjectQueue))
  524. {
  525. if (Prestore)
  526. {
  527. await gameObjectQueue.Preset<T>(clock, isUseSynchronous);
  528. return default;
  529. }
  530. return await gameObjectQueue.Dequeue<T>(isActive, clock, isUseSynchronous);
  531. }
  532. if (!this._dictionary.TryGetValue(poolName, out gameObjectQueue))
  533. {
  534. gameObjectQueue = EnterPool<T>(poolName, prefabName);
  535. }
  536. if (gameObjectQueue == null)
  537. {
  538. return default;
  539. }
  540. if (Prestore)
  541. {
  542. await gameObjectQueue.Preset<T>(clock, isUseSynchronous);
  543. return default;
  544. }
  545. return await gameObjectQueue.Dequeue<T>(isActive, clock, isUseSynchronous);
  546. }
  547. }
  548. catch (Exception e)
  549. {
  550. LogTool.Exception(e);
  551. return default;
  552. }
  553. #else
  554. return default;
  555. #endif
  556. }
  557. #if !COMBAT_SERVER
  558. private Transform _parent;
  559. private GameObjectQueue EnterPool<T>(string poolName, string prefabName) where T : IGObjectPoolInterface
  560. {
  561. if (_parent == null)
  562. {
  563. _parent = new GameObject().transform;
  564. _parent.gameObject.name = "Pool";
  565. }
  566. GameObjectQueue queue = new GameObjectQueue();
  567. queue.Init(poolName, prefabName);
  568. _dictionary.Add(poolName, queue);
  569. return queue;
  570. }
  571. #endif
  572. public void Recycle(IGObjectPoolInterface poolInterface)
  573. {
  574. if (poolInterface == null)
  575. {
  576. return;
  577. }
  578. #if !COMBAT_SERVER
  579. GameObjectQueue queue;
  580. if (!this._dictionary.TryGetValue(poolInterface.poolObjName, out queue))
  581. {
  582. LogTool.Error("不再池子之中 " + poolInterface.poolObjName);
  583. return;
  584. }
  585. queue.Enqueue(poolInterface);
  586. #endif
  587. }
  588. public async CTask Recycle(IGObjectPoolInterface poolInterface, bool isNotAwaitDelay)
  589. {
  590. if (poolInterface == null)
  591. {
  592. return;
  593. }
  594. #if !COMBAT_SERVER
  595. GameObjectQueue queue;
  596. if (!this._dictionary.TryGetValue(poolInterface.poolObjName, out queue))
  597. {
  598. LogTool.Error("不再池子之中 " + poolInterface.poolObjName);
  599. return;
  600. }
  601. await queue.Enqueue(poolInterface, isNotAwaitDelay);
  602. #endif
  603. }
  604. public void RecycleForDestroy(IGObjectPoolInterface poolInterface, bool immediately = false)
  605. {
  606. if (poolInterface == null)
  607. {
  608. return;
  609. }
  610. #if !COMBAT_SERVER
  611. GameObjectQueue queue;
  612. if (!this._dictionary.TryGetValue(poolInterface.poolObjName, out queue))
  613. {
  614. LogTool.Error("不再池子之中 " + poolInterface.poolObjName);
  615. return;
  616. }
  617. queue.Enqueue(poolInterface);
  618. queue.AddDestroy(poolInterface, immediately);
  619. #endif
  620. }
  621. public void DestroyPool(string poolObjName)
  622. {
  623. #if !COMBAT_SERVER
  624. GameObjectQueue queue;
  625. if (!this._dictionary.TryGetValue(poolObjName, out queue))
  626. {
  627. // LogTool.Warning("没有这个池子 " + poolObjName);
  628. return;
  629. }
  630. _dictionary.Remove(poolObjName);
  631. queue.Dispose();
  632. #endif
  633. }
  634. /// <summary>
  635. /// 休眠一个池子
  636. /// </summary>
  637. /// <param name="poolName"></param>
  638. public void DormancyPool<T>(string poolName)
  639. {
  640. #if !COMBAT_SERVER
  641. GameObjectQueue queue;
  642. if (!this._dictionary.TryGetValue(poolName, out queue))
  643. {
  644. string poolObjName = GetPoolName<T>(poolName);
  645. if (!this._dictionary.TryGetValue(poolObjName, out queue))
  646. {
  647. // LogTool.Warning("没有这个池子 " + poolObjName);
  648. return;
  649. }
  650. }
  651. queue?.DormancyPool();
  652. #endif
  653. }
  654. /// <summary>
  655. /// 清除所有池子
  656. /// </summary>
  657. public void Clear()
  658. {
  659. Dispose();
  660. }
  661. /// <summary>
  662. /// 清除所有池子
  663. /// </summary>
  664. public void Dispose()
  665. {
  666. #if !COMBAT_SERVER
  667. foreach (KeyValuePair<string, GameObjectQueue> kv in this._dictionary)
  668. {
  669. kv.Value.Dispose();
  670. }
  671. this._dictionary.Clear();
  672. _instance = null;
  673. #endif
  674. }
  675. }
  676. }