UniPooling.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using YooAsset;
  6. namespace UniFramework.Pooling
  7. {
  8. /// <summary>
  9. /// 游戏对象池系统
  10. /// </summary>
  11. public static class UniPooling
  12. {
  13. private static bool _isInitialize = false;
  14. private static GameObject _driver = null;
  15. private static readonly List<Spawner> _spawners = new List<Spawner>();
  16. /// <summary>
  17. /// 初始化游戏对象池系统
  18. /// </summary>
  19. public static void Initalize()
  20. {
  21. if (_isInitialize)
  22. throw new Exception($"{nameof(UniPooling)} is initialized !");
  23. if (_isInitialize == false)
  24. {
  25. // 创建驱动器
  26. _isInitialize = true;
  27. _driver = new UnityEngine.GameObject($"[{nameof(UniPooling)}]");
  28. _driver.AddComponent<UniPoolingDriver>();
  29. UnityEngine.Object.DontDestroyOnLoad(_driver);
  30. UniLogger.Log($"{nameof(UniPooling)} initalize !");
  31. }
  32. }
  33. /// <summary>
  34. /// 销毁游戏对象池系统
  35. /// </summary>
  36. public static void Destroy()
  37. {
  38. if (_isInitialize)
  39. {
  40. foreach (var spawner in _spawners)
  41. {
  42. spawner.Destroy();
  43. }
  44. _spawners.Clear();
  45. _isInitialize = false;
  46. if (_driver != null)
  47. GameObject.Destroy(_driver);
  48. UniLogger.Log($"{nameof(UniPooling)} destroy all !");
  49. }
  50. }
  51. /// <summary>
  52. /// 更新游戏对象池系统
  53. /// </summary>
  54. internal static void Update()
  55. {
  56. if (_isInitialize)
  57. {
  58. foreach (var spawner in _spawners)
  59. {
  60. spawner.Update();
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// 创建游戏对象生成器
  66. /// </summary>
  67. /// <param name="packageName">资源包名称</param>
  68. public static Spawner CreateSpawner(string packageName)
  69. {
  70. // 获取资源包
  71. var assetPackage = YooAssets.GetPackage(packageName);
  72. if (assetPackage == null)
  73. throw new Exception($"Not found asset package : {packageName}");
  74. // 检测资源包初始化状态
  75. if (assetPackage.InitializeStatus == EOperationStatus.None)
  76. throw new Exception($"Asset package {packageName} not initialize !");
  77. if (assetPackage.InitializeStatus == EOperationStatus.Failed)
  78. throw new Exception($"Asset package {packageName} initialize failed !");
  79. if (HasSpawner(packageName))
  80. return GetSpawner(packageName);
  81. Spawner spawner = new Spawner(_driver, assetPackage);
  82. _spawners.Add(spawner);
  83. return spawner;
  84. }
  85. /// <summary>
  86. /// 获取游戏对象生成器
  87. /// </summary>
  88. /// <param name="packageName">资源包名称</param>
  89. public static Spawner GetSpawner(string packageName)
  90. {
  91. foreach (var spawner in _spawners)
  92. {
  93. if (spawner.PackageName == packageName)
  94. return spawner;
  95. }
  96. UniLogger.Warning($"Not found spawner : {packageName}");
  97. return null;
  98. }
  99. /// <summary>
  100. /// 检测游戏对象生成器是否存在
  101. /// </summary>
  102. /// <param name="packageName">资源包名称</param>
  103. public static bool HasSpawner(string packageName)
  104. {
  105. foreach (var spawner in _spawners)
  106. {
  107. if (spawner.PackageName == packageName)
  108. return true;
  109. }
  110. return false;
  111. }
  112. }
  113. }