yyl
2025-05-13 6208ed5ed833142a459d0dc8ad8622ca8047a80e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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>();
    }
}