using System;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using Cysharp.Threading.Tasks;
|
using System.Reflection;
|
|
|
public class ConfigManager : ManagerBase<ConfigManager>
|
{
|
public bool isLoadFinished
|
{
|
get;
|
private set;
|
}
|
|
private float loadingProgress = 0f;
|
|
public override void Init()
|
{
|
base.Init();
|
InitConfigs();
|
}
|
|
public virtual async UniTask InitConfigs()
|
{
|
// 加载配置文件
|
await LoadConfigs();
|
}
|
|
protected async UniTask LoadConfigs()
|
{
|
loadingProgress = 0f;
|
isLoadFinished = false;
|
|
// 加载配置文件
|
int totalConfigs = 1;
|
Type[] configTypes = new Type[] {
|
typeof(FamilyConfig)
|
};
|
|
// 逐个加载配置并更新进度
|
for (int i = 0; i < configTypes.Length; i++)
|
{
|
await LoadConfigByType(configTypes[i]);
|
loadingProgress = (float)(i + 1) / totalConfigs;
|
}
|
|
// 加载完成后设置isLoadFinished为true
|
loadingProgress = 1f;
|
isLoadFinished = true;
|
}
|
|
private async UniTask LoadConfigByType(Type configType)
|
{
|
string configName = configType.Name;
|
TextAsset textAsset = await ResManager.Instance.LoadAsset<TextAsset>("Config", configName);
|
if (textAsset != null)
|
{
|
string[] lines = textAsset.text.Split('\n');
|
var methodInfo = configType.GetMethod("Init", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
|
if (methodInfo != null)
|
{
|
methodInfo.Invoke(null, new object[] { lines });
|
// 设置初始化标志
|
var isInitField = configType.GetField("isInit", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
if (isInitField != null)
|
{
|
isInitField.SetValue(null, true);
|
}
|
Debug.Log($"加载配置: {configType.Name} 成功");
|
}
|
else
|
{
|
Debug.LogError($"配置类 {configType.Name} 没有静态Init方法");
|
}
|
}
|
else
|
{
|
Debug.LogError($"找不到配置文件: {configName}");
|
}
|
}
|
|
private async UniTask LoadConfig<T>() where T : class
|
{
|
string configName = typeof(T).Name;
|
|
TextAsset textAsset = await ResManager.Instance.LoadAsset<TextAsset>("Config", configName);
|
if (textAsset != null)
|
{
|
string[] lines = textAsset.text.Split('\n');
|
var methodInfo = typeof(T).GetMethod("Init", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
|
if (methodInfo != null)
|
{
|
methodInfo.Invoke(null, new object[] { lines });
|
// 设置初始化标志
|
var isInitField = typeof(T).GetField("isInit", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
if (isInitField != null)
|
{
|
isInitField.SetValue(null, true);
|
}
|
Debug.Log($"加载配置: {typeof(T).Name} 成功");
|
}
|
else
|
{
|
Debug.LogError($"配置类 {typeof(T).Name} 没有静态Init方法");
|
}
|
}
|
else
|
{
|
Debug.LogError($"找不到配置文件: {configName}");
|
}
|
}
|
|
public float GetLoadingProgress()
|
{
|
return loadingProgress;
|
}
|
|
private void ClearConfigDictionary<T>() where T : class
|
{
|
// 重置 T 初始化状态
|
var isInitField = typeof(T).GetField("isInit", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
if (isInitField != null)
|
{
|
isInitField.SetValue(null, false);
|
}
|
}
|
|
public override void Release()
|
{
|
// 清空 FamilyConfig 字典
|
ClearConfigDictionary<FamilyConfig>();
|
}
|
}
|