CommonDataManager.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using SimpleJSON;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using ET;
  6. using GFG.HotUpdate.LubanConfig;
  7. using GFGGame;
  8. using YooAsset;
  9. public static class CommonDataManager
  10. {
  11. public static cfg.Tables Tables;
  12. private static Dictionary<string, JSONNode> _jsonCache = new Dictionary<string, JSONNode>();
  13. private static int _totalFilesToLoad;
  14. private static int _loadedFilesCount;
  15. public static void InitLoginConfigs(System.Action onComplete)
  16. {
  17. // 所有需要加载的配置表文件列表
  18. var filesToLoad = LubanConfigFiles.GetLogInLoadConfigFiles();
  19. _totalFilesToLoad = filesToLoad.Count;
  20. _loadedFilesCount = 0;
  21. foreach (var file in filesToLoad)
  22. {
  23. LoadConfigTextAsset(file, () =>
  24. {
  25. _loadedFilesCount++;
  26. if (_loadedFilesCount == _totalFilesToLoad)
  27. {
  28. // 所有文件加载完成后初始化Tables
  29. Tables = new cfg.Tables(GetCachedJson);
  30. onComplete?.Invoke();
  31. }
  32. });
  33. }
  34. }
  35. public static void InitAllAsync(System.Action onComplete)
  36. {
  37. // 所有需要加载的配置表文件列表
  38. var filesToLoad = LubanConfigFiles.GetFilesToLoad();
  39. _totalFilesToLoad = filesToLoad.Count;
  40. _loadedFilesCount = 0;
  41. foreach (var file in filesToLoad)
  42. {
  43. LoadConfigTextAsset(file, () =>
  44. {
  45. _loadedFilesCount++;
  46. if (_loadedFilesCount == _totalFilesToLoad)
  47. {
  48. // 所有文件加载完成后初始化Tables
  49. Tables = new cfg.Tables(GetCachedJson);
  50. onComplete?.Invoke();
  51. }
  52. });
  53. }
  54. }
  55. private static JSONNode GetCachedJson(string file)
  56. {
  57. lock (_jsonCache)
  58. {
  59. if (_jsonCache.TryGetValue(file, out var json))
  60. {
  61. return json;
  62. }
  63. return null;
  64. }
  65. }
  66. private static async Task<TextAsset> LoadConfigAsync(string resPath)
  67. {
  68. AssetHandle handle = YooAssets.LoadAssetAsync<TextAsset>(resPath);
  69. await handle.Task;
  70. return handle.AssetObject as TextAsset;
  71. }
  72. private static async Task LoadConfigTextAsset(string file, System.Action onLoaded)
  73. {
  74. var url = ResPathUtil.GetConfigPath(file);
  75. TextAsset textAsset = await LoadConfigAsync(url); // 异步加载文件
  76. JSONNode jsonNode = JSON.Parse(textAsset.text);
  77. lock (_jsonCache)
  78. {
  79. _jsonCache[file] = jsonNode; // 直接存储 JSONNode,避免重复解析
  80. }
  81. onLoaded?.Invoke(); // 执行回调
  82. }
  83. }