Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / ResourceManager.cs
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. public static class ResourceManager
  5. {
  6. /// <summary>
  7. /// 颜色混合材质
  8. /// </summary>
  9. public static ShaderMaterial BlendMaterial
  10. {
  11. get
  12. {
  13. if (_shadowMaterial == null)
  14. {
  15. _shadowMaterial = ResourceLoader.Load<ShaderMaterial>(ResourcePath.resource_materlal_Blend_tres);
  16. }
  17.  
  18. return _shadowMaterial;
  19. }
  20. }
  21.  
  22. private static ShaderMaterial _shadowMaterial;
  23.  
  24. /// <summary>
  25. /// 颜色混合Shader
  26. /// </summary>
  27. public static Shader BlendShader
  28. {
  29. get
  30. {
  31. if (_shadowShader == null)
  32. {
  33. _shadowShader = ResourceLoader.Load<Shader>(ResourcePath.resource_materlal_Blend_gdshader);
  34. }
  35.  
  36. return _shadowShader;
  37. }
  38. }
  39.  
  40. private static Shader _shadowShader;
  41.  
  42. private static readonly Dictionary<string, object> CachePack = new Dictionary<string, object>();
  43.  
  44. /// <summary>
  45. /// 加载资源对象, 并且缓存当前资源对象, 可频繁获取
  46. /// </summary>
  47. /// <param name="path">资源路径</param>
  48. /// <param name="useCache">是否使用缓存中的资源</param>
  49. public static T Load<T>(string path, bool useCache = true) where T : class
  50. {
  51. if (!useCache)
  52. {
  53. T res = ResourceLoader.Load<T>(path, null, ResourceLoader.CacheMode.Ignore);
  54. if (res == null)
  55. {
  56. GD.PrintErr("加载资源失败, 未找到资源: " + path);
  57. return default;
  58. }
  59.  
  60. return res;
  61. }
  62.  
  63. if (CachePack.TryGetValue(path, out var pack))
  64. {
  65. return pack as T;
  66. }
  67.  
  68. pack = ResourceLoader.Load<T>(path);
  69. if (pack != null)
  70. {
  71. CachePack.Add(path, pack);
  72. return pack as T;
  73. }
  74. else
  75. {
  76. GD.PrintErr("加载资源失败, 未找到资源: " + path);
  77. }
  78.  
  79. return default;
  80. }
  81. }