Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / ObjectManager.cs
@小李xl 小李xl on 8 Nov 2023 1 KB 给特效添加对象池功能
  1.  
  2. using Godot;
  3.  
  4. public static class ObjectManager
  5. {
  6. /// <summary>
  7. /// 根据资源路径获取实例对象, 该对象必须实现 IPoolItem 接口
  8. /// </summary>
  9. /// <param name="resPath">资源路径</param>
  10. public static IPoolItem GetPoolItem(string resPath)
  11. {
  12. var item = ObjectPool.GetItem(resPath);
  13. if (item == null)
  14. {
  15. item = (IPoolItem)ResourceManager.LoadAndInstantiate<Node>(resPath);
  16. item.Logotype = resPath;
  17. }
  18.  
  19. return item;
  20. }
  21. /// <summary>
  22. /// 根据资源路径获取实例对象, 该对象必须实现 IPoolItem 接口
  23. /// </summary>
  24. /// <param name="resPath">资源路径</param>
  25. public static T GetPoolItem<T>(string resPath) where T : IPoolItem
  26. {
  27. var item = ObjectPool.GetItem<T>(resPath);
  28. if (item == null)
  29. {
  30. item = (T)(IPoolItem)ResourceManager.LoadAndInstantiate(resPath);
  31. item.Logotype = resPath;
  32. }
  33.  
  34. return item;
  35. }
  36.  
  37. public static Bullet GetBullet(string id)
  38. {
  39. var bullet = ObjectPool.GetItem<Bullet>(id);
  40. if (bullet == null)
  41. {
  42. bullet = ActivityObject.Create<Bullet>(id);
  43. bullet.Logotype = id;
  44. }
  45.  
  46. return bullet;
  47. }
  48. public static Laser GetLaser(string resPath)
  49. {
  50. var bullet = ObjectPool.GetItem<Laser>(resPath);
  51. if (bullet == null)
  52. {
  53. bullet = ResourceManager.LoadAndInstantiate<Laser>(resPath);
  54. bullet.Logotype = resPath;
  55. }
  56.  
  57. return bullet;
  58. }
  59. }