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