Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / ResourceManager.cs
  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 T LoadAndInstantiate<T>(string path, bool useCache = true) where T : Node
  130. {
  131. var packedScene = Load<PackedScene>(path, useCache);
  132. return packedScene.Instantiate<T>();
  133. }
  134.  
  135. /// <summary>
  136. /// 读取文本资源
  137. /// </summary>
  138. public static string LoadText(string path)
  139. {
  140. string text;
  141. using (var fileAccess = FileAccess.Open(path, FileAccess.ModeFlags.Read))
  142. {
  143. text = fileAccess.GetAsText();
  144. }
  145. return text;
  146. }
  147.  
  148. /// <summary>
  149. /// 加载2d纹理资源
  150. /// </summary>
  151. /// <param name="path">资源路径</param>
  152. /// <param name="useCache">是否使用缓存中的资源</param>
  153. public static Texture2D LoadTexture2D(string path, bool useCache = true)
  154. {
  155. return Load<Texture2D>(path, useCache);
  156. }
  157. /// <summary>
  158. /// 将普通路径转化为 Godot 资源路径
  159. /// </summary>
  160. public static string ToResPath(string path)
  161. {
  162. var field = path.Replace("\\", "/");
  163. return "res://" + field;
  164. }
  165.  
  166. /// <summary>
  167. /// 移除资源后缀名
  168. /// </summary>
  169. public static string RemoveExtension(string name)
  170. {
  171. var index = name.LastIndexOf(".", StringComparison.Ordinal);
  172. if (index >= 0)
  173. {
  174. return name.Substring(0, index);
  175. }
  176.  
  177. return name;
  178. }
  179. }