Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / ResourceManager.cs
@小李xl 小李xl on 8 Nov 2023 5 KB 给特效添加对象池功能
  1. using System;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 资源管理器
  7. /// </summary>
  8. public static class ResourceManager
  9. {
  10. /// <summary>
  11. /// 默认权重值
  12. /// </summary>
  13. public const int DefaultWeight = 100;
  14. /// <summary>
  15. /// 颜色混合材质
  16. /// </summary>
  17. public static ShaderMaterial BlendMaterial
  18. {
  19. get
  20. {
  21. if (_shadowMaterial == null)
  22. {
  23. _shadowMaterial = Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres);
  24. }
  25.  
  26. return _shadowMaterial;
  27. }
  28. }
  29.  
  30. private static ShaderMaterial _shadowMaterial;
  31. public static ShaderMaterial NewBlendMaterial => Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  32.  
  33. /// <summary>
  34. /// 颜色混合Shader
  35. /// </summary>
  36. public static Shader BlendShader
  37. {
  38. get
  39. {
  40. if (_shadowShader == null)
  41. {
  42. _shadowShader = Load<Shader>(ResourcePath.resource_material_Blend_tres);
  43. }
  44.  
  45. return _shadowShader;
  46. }
  47. }
  48.  
  49. private static Shader _shadowShader;
  50.  
  51. /// <summary>
  52. /// 默认字体资源, 字体大小16px
  53. /// </summary>
  54. public static Font DefaultFont16Px
  55. {
  56. get
  57. {
  58. if (_defaultFont16Px == null)
  59. {
  60. _defaultFont16Px = Load<Font>(ResourcePath.resource_font_VonwaonBitmap16px_ttf);
  61. }
  62.  
  63. return _defaultFont16Px;
  64. }
  65. }
  66. private static Font _defaultFont16Px;
  67. /// <summary>
  68. /// 默认字体资源, 字体大小12px
  69. /// </summary>
  70. public static Font DefaultFont12Px
  71. {
  72. get
  73. {
  74. if (_defaultFont12Px == null)
  75. {
  76. _defaultFont12Px = Load<Font>(ResourcePath.resource_font_VonwaonBitmap12px_ttf);
  77. }
  78.  
  79. return _defaultFont12Px;
  80. }
  81. }
  82. private static Font _defaultFont12Px;
  83. //缓存的资源
  84. private static readonly Dictionary<string, object> CachePack = new Dictionary<string, object>();
  85.  
  86. /// <summary>
  87. /// 加载资源对象, 并且缓存当前资源对象, 可频繁获取
  88. /// </summary>
  89. /// <param name="path">资源路径</param>
  90. /// <param name="useCache">是否使用缓存中的资源</param>
  91. public static T Load<T>(string path, bool useCache = true) where T : class
  92. {
  93. if (!useCache)
  94. {
  95. var res = ResourceLoader.Load<T>(path, null, ResourceLoader.CacheMode.Ignore);
  96. if (res == null)
  97. {
  98. Debug.LogError("加载资源失败, 未找到资源: " + path);
  99. return default;
  100. }
  101.  
  102. return res;
  103. }
  104.  
  105. if (CachePack.TryGetValue(path, out var pack))
  106. {
  107. return pack as T;
  108. }
  109.  
  110. pack = ResourceLoader.Load<T>(path);
  111. if (pack != null)
  112. {
  113. CachePack.Add(path, pack);
  114. return pack as T;
  115. }
  116. else
  117. {
  118. Debug.LogError("加载资源失败, 未找到资源: " + path);
  119. }
  120.  
  121. return default;
  122. }
  123.  
  124. /// <summary>
  125. /// 加载并且实例化场景, 并返回
  126. /// </summary>
  127. /// <param name="path">场景路径</param>
  128. /// <param name="useCache">是否使用缓存中的资源</param>
  129. public static Node LoadAndInstantiate(string path, bool useCache = true)
  130. {
  131. var packedScene = Load<PackedScene>(path, useCache);
  132. return packedScene.Instantiate();
  133. }
  134. /// <summary>
  135. /// 加载并且实例化场景, 并返回
  136. /// </summary>
  137. /// <param name="path">场景路径</param>
  138. /// <param name="useCache">是否使用缓存中的资源</param>
  139. public static T LoadAndInstantiate<T>(string path, bool useCache = true) where T : Node
  140. {
  141. var packedScene = Load<PackedScene>(path, useCache);
  142. return packedScene.Instantiate<T>();
  143. }
  144.  
  145. /// <summary>
  146. /// 读取文本资源
  147. /// </summary>
  148. public static string LoadText(string path)
  149. {
  150. string text;
  151. using (var fileAccess = FileAccess.Open(path, FileAccess.ModeFlags.Read))
  152. {
  153. text = fileAccess.GetAsText();
  154. }
  155. return text;
  156. }
  157.  
  158. /// <summary>
  159. /// 加载2d纹理资源
  160. /// </summary>
  161. /// <param name="path">资源路径</param>
  162. /// <param name="useCache">是否使用缓存中的资源</param>
  163. public static Texture2D LoadTexture2D(string path, bool useCache = true)
  164. {
  165. return Load<Texture2D>(path, useCache);
  166. }
  167. /// <summary>
  168. /// 将普通路径转化为 Godot 资源路径
  169. /// </summary>
  170. public static string ToResPath(string path)
  171. {
  172. var field = path.Replace("\\", "/");
  173. return "res://" + field;
  174. }
  175.  
  176. /// <summary>
  177. /// 移除资源后缀名
  178. /// </summary>
  179. public static string RemoveExtension(string name)
  180. {
  181. var index = name.LastIndexOf(".", StringComparison.Ordinal);
  182. if (index >= 0)
  183. {
  184. return name.Substring(0, index);
  185. }
  186.  
  187. return name;
  188. }
  189. }