CallPool.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /****************************************************************************
  2. * Copyright (c) 2018 ~ 2022 UNDER MIT Lisence 布鞋 827922094@qq.com
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. ****************************************************************************/
  7. using UnityEngine;
  8. namespace QFramework
  9. {
  10. public class CallPool : MonoBehaviour
  11. {
  12. class Fish
  13. {
  14. }
  15. private void Start()
  16. {
  17. #region SimpleObjectPool
  18. var pool = new SimpleObjectPool<Fish>(() => new Fish(),initCount:50);
  19. Debug.Log(pool.CurCount);
  20. var fish = pool.Allocate();
  21. Debug.Log(pool.CurCount);
  22. pool.Recycle(fish);
  23. Debug.Log(pool.CurCount);
  24. #endregion
  25. #region SafeObjectPool
  26. SafeObjectPool<Bullet>.Instance.Init(50,25);
  27. var bullet = Bullet.Allocate();
  28. Debug.Log(SafeObjectPool<Bullet>.Instance.CurCount);
  29. bullet.Recycle2Cache();
  30. Debug.Log(SafeObjectPool<Bullet>.Instance.CurCount);
  31. #endregion
  32. }
  33. class Bullet :IPoolable,IPoolType
  34. {
  35. public void OnRecycled()
  36. {
  37. Debug.Log("回收了");
  38. }
  39. public bool IsRecycled { get; set; }
  40. public static Bullet Allocate()
  41. {
  42. return SafeObjectPool<Bullet>.Instance.Allocate();
  43. }
  44. public void Recycle2Cache()
  45. {
  46. SafeObjectPool<Bullet>.Instance.Recycle(this);
  47. }
  48. }
  49. }
  50. }
  51. // 50
  52. // 49
  53. // 50
  54. // 回收了 x 25
  55. // 24
  56. // 回收了 24
  57. // 回收了
  58. // 回收了 25