115 lines
3.2 KiB
C#
115 lines
3.2 KiB
C#
using cfg;
|
||
using cfg.StringCfg;
|
||
using Cysharp.Threading.Tasks;
|
||
using PhxhSDK;
|
||
using SimpleJSON;
|
||
using UnityEngine;
|
||
|
||
|
||
namespace Framework
|
||
{
|
||
public class TableManager : Singlenton<TableManager>, IInitable, ILoadable
|
||
{
|
||
private Tables _tables;
|
||
public Tables Tables
|
||
{
|
||
get
|
||
{
|
||
#if UNITY_EDITOR
|
||
if (_tables == null && !Application.isPlaying)
|
||
{
|
||
_tables = EditorTableManager.instance.tables;
|
||
}
|
||
#endif
|
||
return _tables;
|
||
}
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
}
|
||
|
||
public static JSONNode LoadByteBuf(string file)
|
||
{
|
||
// DebugUtil.LogY("TableManager LoadByteBuf {0}", file);
|
||
var filePath = _CombinePath(file);
|
||
var loadAsset = AssetManager.Instance.GetPreLoadResult<TextAsset>(filePath);
|
||
var jsonText = loadAsset.text;
|
||
return JSON.Parse(jsonText);
|
||
}
|
||
|
||
private static string _CombinePath(string fileName)
|
||
{
|
||
return string.Format(Constants.DATA_FORMAT_PATH, fileName);
|
||
}
|
||
|
||
public async UniTask Load()
|
||
{
|
||
try
|
||
{
|
||
if (_tables != null)
|
||
{
|
||
return;
|
||
}
|
||
// 先预加载
|
||
Tables.PostPreload(_PostPreload);
|
||
await AssetManager.Instance.WaitAllPreloads();
|
||
// 再加载
|
||
_tables = new Tables(LoadByteBuf);
|
||
//生成多语言
|
||
_LoadStringCfgAsync();
|
||
await StringManager.LoadMixKeyDic();
|
||
// 再释放
|
||
Tables.AfterLoaded(_UnlockJson);
|
||
|
||
EventManager.Instance.Send(EventManager.EventName.AllConfigLoad);
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
DebugUtil.LogError($"Load Failed,exception:{e.Message} \n {e.StackTrace}");
|
||
}
|
||
}
|
||
|
||
public void UnLoad()
|
||
{
|
||
|
||
}
|
||
|
||
private void _UnlockJson(string fileName)
|
||
{
|
||
var fullPath = _CombinePath(fileName);
|
||
AssetManager.Instance.Unload(fullPath);
|
||
}
|
||
|
||
private void _PostPreload(string fileName)
|
||
{
|
||
var fullPath = _CombinePath(fileName);
|
||
AssetManager.Instance.PostPreload(fullPath);
|
||
}
|
||
|
||
private async void _LoadStringCfgAsync()
|
||
{
|
||
LanguageManager.Instance.SetCurrentLanguage(LanguageManager.LanguageType.CN);
|
||
var path = StringManager.GetLocalizeJsonPath();
|
||
var loadAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(path);
|
||
if (loadAsset)
|
||
{
|
||
var jsonText = loadAsset.text;
|
||
var jsonNode = JSON.Parse(jsonText);
|
||
StringManager.StringConfig = new StringConfig(jsonNode);
|
||
return;
|
||
}
|
||
DebugUtil.LogError("多语言JsonNode未创建成功,请检查路径!" + path);
|
||
}
|
||
|
||
public UniTask Unload()
|
||
{
|
||
return UniTask.NextFrame();
|
||
}
|
||
}
|
||
}
|