| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using SimpleJSON;
- using UnityEngine;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using ET;
- using GFG.HotUpdate.LubanConfig;
- using GFGGame;
- using YooAsset;
- public static class CommonDataManager
- {
- public static cfg.Tables Tables;
- private static Dictionary<string, JSONNode> _jsonCache = new Dictionary<string, JSONNode>();
- private static int _totalFilesToLoad;
- private static int _loadedFilesCount;
- public static void InitLoginConfigs(System.Action onComplete)
- {
- // 所有需要加载的配置表文件列表
- var filesToLoad = LubanConfigFiles.GetLogInLoadConfigFiles();
- _totalFilesToLoad = filesToLoad.Count;
- _loadedFilesCount = 0;
- foreach (var file in filesToLoad)
- {
- LoadConfigTextAsset(file, () =>
- {
- _loadedFilesCount++;
- if (_loadedFilesCount == _totalFilesToLoad)
- {
- // 所有文件加载完成后初始化Tables
- Tables = new cfg.Tables(GetCachedJson);
- onComplete?.Invoke();
- }
- });
- }
- }
- public static void InitAllAsync(System.Action onComplete)
- {
- // 所有需要加载的配置表文件列表
- var filesToLoad = LubanConfigFiles.GetFilesToLoad();
- _totalFilesToLoad = filesToLoad.Count;
- _loadedFilesCount = 0;
- foreach (var file in filesToLoad)
- {
- LoadConfigTextAsset(file, () =>
- {
- _loadedFilesCount++;
- if (_loadedFilesCount == _totalFilesToLoad)
- {
- // 所有文件加载完成后初始化Tables
- Tables = new cfg.Tables(GetCachedJson);
- onComplete?.Invoke();
- }
- });
- }
- }
- private static JSONNode GetCachedJson(string file)
- {
- lock (_jsonCache)
- {
- if (_jsonCache.TryGetValue(file, out var json))
- {
- return json;
- }
- return null;
- }
- }
- private static async Task<TextAsset> LoadConfigAsync(string resPath)
- {
- AssetHandle handle = YooAssets.LoadAssetAsync<TextAsset>(resPath);
- await handle.Task;
- return handle.AssetObject as TextAsset;
- }
- private static async Task LoadConfigTextAsset(string file, System.Action onLoaded)
- {
- var url = ResPathUtil.GetConfigPath(file);
- TextAsset textAsset = await LoadConfigAsync(url); // 异步加载文件
- JSONNode jsonNode = JSON.Parse(textAsset.text);
- lock (_jsonCache)
- {
- _jsonCache[file] = jsonNode; // 直接存储 JSONNode,避免重复解析
- }
- onLoaded?.Invoke(); // 执行回调
- }
- }
|