using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using UnityEditor;
|
using UnityEngine;
|
|
/// <summary>
|
/// 配置生成器 - 用于自动生成配置管理器代码
|
/// </summary>
|
public static class ConfigGenerater
|
{
|
// 常量定义
|
private const string ConfigsPath = "Assets/Scripts/Main/Config/Configs";
|
private const string ConfigManagerPath = "Assets/Scripts/Main/Manager/ConfigManager.cs";
|
|
/// <summary>
|
/// 生成配置管理器菜单项
|
/// </summary>
|
[MenuItem("Tools/生成配置管理器")]
|
public static void Generate()
|
{
|
try
|
{
|
// 获取所有配置类
|
List<string> configClasses = GetAllConfigClasses();
|
|
// 生成配置管理器代码
|
GenerateConfigManager(configClasses);
|
|
Debug.Log($"配置管理器生成完成,共找到 {configClasses.Count} 个配置类: {string.Join(", ", configClasses)}");
|
|
// 刷新资源
|
AssetDatabase.Refresh();
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError($"生成配置管理器时发生错误: {ex.Message}\n{ex.StackTrace}");
|
}
|
}
|
|
/// <summary>
|
/// 获取所有配置类
|
/// </summary>
|
/// <returns>配置类名称列表</returns>
|
private static List<string> GetAllConfigClasses()
|
{
|
// 获取配置目录的完整路径
|
string fullConfigsPath = Path.Combine(Application.dataPath, ConfigsPath.Replace("Assets/", ""));
|
|
// 检查目录是否存在
|
if (!Directory.Exists(fullConfigsPath))
|
{
|
Debug.LogError($"配置目录不存在: {fullConfigsPath}");
|
return new List<string>();
|
}
|
|
try
|
{
|
List<string> configClasses = new List<string>();
|
|
// 获取所有C#文件
|
string[] files = Directory.GetFiles(fullConfigsPath, "*.cs", SearchOption.TopDirectoryOnly);
|
|
for (int i = 0; i < files.Length; i++)
|
{
|
string file = files[i];
|
configClasses.Add(Path.GetFileNameWithoutExtension(file));
|
}
|
|
return configClasses;
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError($"扫描配置类时发生错误: {ex.Message}");
|
return new List<string>();
|
}
|
}
|
|
/// <summary>
|
/// 生成配置管理器代码
|
/// </summary>
|
/// <param name="configClasses">配置类列表</param>
|
private static void GenerateConfigManager(List<string> configClasses)
|
{
|
// 获取配置管理器的完整路径
|
string fullConfigManagerPath = Path.Combine(Application.dataPath, ConfigManagerPath.Replace("Assets/", ""));
|
|
try
|
{
|
// 生成完整的配置管理器代码
|
string configManagerCode = GenerateFullConfigManagerCode(configClasses);
|
|
// 写入文件
|
File.WriteAllText(fullConfigManagerPath, configManagerCode);
|
|
Debug.Log($"成功生成配置管理器: {fullConfigManagerPath}");
|
}
|
catch (Exception ex)
|
{
|
Debug.LogError($"生成配置管理器文件时发生错误: {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 生成完整的配置管理器代码
|
/// </summary>
|
/// <param name="configClasses">配置类列表</param>
|
/// <returns>完整的配置管理器代码</returns>
|
private static string GenerateFullConfigManagerCode(List<string> configClasses)
|
{
|
StringBuilder sb = new StringBuilder();
|
|
// 添加命名空间引用
|
sb.AppendLine("using System;");
|
sb.AppendLine("using System.Collections.Generic;");
|
sb.AppendLine("using UnityEngine;");
|
sb.AppendLine("using Cysharp.Threading.Tasks;");
|
sb.AppendLine("using System.Reflection;");
|
sb.AppendLine();
|
sb.AppendLine();
|
sb.AppendLine("public class ConfigManager : ManagerBase<ConfigManager>");
|
sb.AppendLine("{");
|
|
// 添加属性和字段
|
sb.AppendLine(" public bool isLoadFinished");
|
sb.AppendLine(" {");
|
sb.AppendLine(" get;");
|
sb.AppendLine(" private set;");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
sb.AppendLine(" private float loadingProgress = 0f;");
|
sb.AppendLine();
|
|
// 添加初始化方法
|
sb.AppendLine(" public override void Init()");
|
sb.AppendLine(" {");
|
sb.AppendLine(" base.Init();");
|
sb.AppendLine(" InitConfigs();");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加配置初始化方法
|
sb.AppendLine(" public virtual async UniTask InitConfigs()");
|
sb.AppendLine(" {");
|
sb.AppendLine(" // 加载配置文件");
|
sb.AppendLine(" await LoadConfigs();");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加LoadConfigs方法
|
sb.AppendLine(" protected async UniTask LoadConfigs()");
|
sb.AppendLine(" {");
|
sb.AppendLine(" loadingProgress = 0f;");
|
sb.AppendLine(" isLoadFinished = false;");
|
sb.AppendLine();
|
sb.AppendLine(" // 加载配置文件");
|
|
// 如果有配置类,添加进度跟踪代码
|
if (configClasses.Count > 0)
|
{
|
sb.AppendLine($" int totalConfigs = {configClasses.Count};");
|
|
// 使用数组和循环加载配置
|
sb.AppendLine(" Type[] configTypes = new Type[] {");
|
for (int i = 0; i < configClasses.Count; i++)
|
{
|
sb.Append($" typeof({configClasses[i]})");
|
sb.AppendLine(i < configClasses.Count - 1 ? "," : "");
|
}
|
sb.AppendLine(" };");
|
sb.AppendLine();
|
sb.AppendLine(" // 逐个加载配置并更新进度");
|
sb.AppendLine(" for (int i = 0; i < configTypes.Length; i++)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" await LoadConfigByType(configTypes[i]);");
|
sb.AppendLine(" loadingProgress = (float)(i + 1) / totalConfigs;");
|
sb.AppendLine(" }");
|
}
|
else
|
{
|
Debug.LogError("没有找到配置类 : " + configClasses.Count);
|
}
|
|
sb.AppendLine();
|
sb.AppendLine(" // 加载完成后设置isLoadFinished为true");
|
sb.AppendLine(" loadingProgress = 1f;");
|
sb.AppendLine(" isLoadFinished = true;");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加LoadConfigByType方法
|
sb.AppendLine(" private async UniTask LoadConfigByType(Type configType)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" string configName = configType.Name;");
|
sb.AppendLine(" TextAsset textAsset = await ResManager.Instance.LoadAsset<TextAsset>(\"Config\", configName);");
|
sb.AppendLine(" if (textAsset != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" string[] lines = textAsset.text.Split('\\n');");
|
sb.AppendLine(" var methodInfo = configType.GetMethod(\"Init\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);");
|
sb.AppendLine(" if (methodInfo != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" methodInfo.Invoke(null, new object[] { lines });");
|
sb.AppendLine(" // 设置初始化标志");
|
sb.AppendLine(" var isInitField = configType.GetField(\"isInit\", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);");
|
sb.AppendLine(" if (isInitField != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" isInitField.SetValue(null, true);");
|
sb.AppendLine(" }");
|
sb.AppendLine(" Debug.Log($\"加载配置: {configType.Name} 成功\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" else");
|
sb.AppendLine(" {");
|
sb.AppendLine(" Debug.LogError($\"配置类 {configType.Name} 没有静态Init方法\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
sb.AppendLine(" else");
|
sb.AppendLine(" {");
|
sb.AppendLine(" Debug.LogError($\"找不到配置文件: {configName}\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加泛型LoadConfig方法
|
sb.AppendLine(" private async UniTask LoadConfig<T>() where T : class");
|
sb.AppendLine(" {");
|
sb.AppendLine(" string configName = typeof(T).Name;");
|
sb.AppendLine();
|
sb.AppendLine(" TextAsset textAsset = await ResManager.Instance.LoadAsset<TextAsset>(\"Config\", configName);");
|
sb.AppendLine(" if (textAsset != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" string[] lines = textAsset.text.Split('\\n');");
|
sb.AppendLine(" var methodInfo = typeof(T).GetMethod(\"Init\", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);");
|
sb.AppendLine(" if (methodInfo != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" methodInfo.Invoke(null, new object[] { lines });");
|
sb.AppendLine(" // 设置初始化标志");
|
sb.AppendLine(" var isInitField = typeof(T).GetField(\"isInit\", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);");
|
sb.AppendLine(" if (isInitField != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" isInitField.SetValue(null, true);");
|
sb.AppendLine(" }");
|
sb.AppendLine(" Debug.Log($\"加载配置: {typeof(T).Name} 成功\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" else");
|
sb.AppendLine(" {");
|
sb.AppendLine(" Debug.LogError($\"配置类 {typeof(T).Name} 没有静态Init方法\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
sb.AppendLine(" else");
|
sb.AppendLine(" {");
|
sb.AppendLine(" Debug.LogError($\"找不到配置文件: {configName}\");");
|
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加GetLoadingProgress方法
|
sb.AppendLine(" public float GetLoadingProgress()");
|
sb.AppendLine(" {");
|
sb.AppendLine(" return loadingProgress;");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加ClearConfigDictionary方法
|
sb.AppendLine(" private void ClearConfigDictionary<T>() where T : class");
|
sb.AppendLine(" {");
|
sb.AppendLine(" // 重置 T 初始化状态");
|
sb.AppendLine(" var isInitField = typeof(T).GetField(\"isInit\", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);");
|
sb.AppendLine(" if (isInitField != null)");
|
sb.AppendLine(" {");
|
sb.AppendLine(" isInitField.SetValue(null, false);");
|
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
sb.AppendLine();
|
|
// 添加Release方法
|
sb.AppendLine(" public override void Release()");
|
sb.AppendLine(" {");
|
|
if (configClasses.Count > 0)
|
{
|
foreach (string className in configClasses)
|
{
|
// 清空字典
|
sb.AppendLine($" // 清空 {className} 字典");
|
sb.AppendLine($" ClearConfigDictionary<{className}>();");
|
}
|
}
|
else
|
{
|
sb.AppendLine(" // 没有找到配置类");
|
}
|
|
sb.AppendLine(" }");
|
|
// 结束类定义
|
sb.AppendLine("}");
|
|
return sb.ToString();
|
}
|
}
|