(client) feat:添加预制件脚本,规范化Dev菜单加载

This commit is contained in:
m0_75251201
2025-07-19 18:47:37 +08:00
parent 0c8eebdd4b
commit 996b2d8a9f
11 changed files with 242 additions and 210 deletions

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Data;
using UnityEngine;
using Utils;
@ -27,12 +29,20 @@ namespace Managers
/// </remarks>
public void Init()
{
if (packs.Count > 0)
return;
// 单线程
var packFolder = Configs.ConfigProcessor.GetSubFolders(new(dataSetFilePath));
foreach (var folder in packFolder)
{
var pack = new DefinePack();
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
if (pack.LoadPack(folder))
{
packs.Add(pack.packID, pack);
}
}
Dictionary<Type, FieldInfo[]> fieldCache = new();
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
@ -102,7 +112,14 @@ namespace Managers
{
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
}
Debug.Log(anonymousDefines.Count);
}
public void Reload()
{
defines.Clear();
packs.Clear();
anonymousDefines.Clear();
Init();
}
/// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。
@ -158,36 +175,6 @@ namespace Managers
return defineList.ToArray();
}
/// <summary>
/// 查询 Define 对象。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <param name="defineName">定义名(内层字典的键)。</param>
/// <returns>如果找到,则返回 Define 对象;否则返回 null。</returns>
public Define QueryDefine(string defineType, string defineName)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
if (string.IsNullOrEmpty(defineName))
{
Debug.LogError("查询失败:定义名参数不能为空!");
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
if (!typeDefinitions.TryGetValue(defineName, out var targetDefine))
{
Debug.LogWarning($"查询失败:定义类型 '{defineType}' 中未找到定义名 '{defineName}'");
return null;
}
return targetDefine;
}
/// <summary>
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
/// </summary>
@ -264,6 +251,78 @@ namespace Managers
return null;
}
}
/// <summary>
/// 查询指定类型下的所有 Define 对象(仅包含命名定义,不包括匿名定义)。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
public Define[] QueryNamedDefinesByType(string defineType)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
List<Define> result = new List<Define>();
// 仅从命名定义中查询
if (defines.TryGetValue(defineType, out var namedDefinitions))
{
result.AddRange(namedDefinitions.Values);
}
// 如果结果为空,则返回 null
if (result.Count == 0)
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}' 的命名定义");
return null;
}
return result.ToArray();
}
/// <summary>
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型(仅包含命名定义,不包括匿名定义)。
/// </summary>
/// <typeparam name="T">目标类型。</typeparam>
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
public T[] QueryNamedDefinesByType<T>()
{
var defineType = typeof(T).Name;
List<Define> allDefines = QueryNamedDefinesByType(defineType)?.ToList();
if (allDefines == null || allDefines.Count == 0)
{
return null;
}
try
{
// 尝试将所有 Define 对象转换为目标类型 T
var result = new List<T>();
foreach (var item in allDefines)
{
if (item is T converted)
{
result.Add(converted);
}
else
{
Debug.LogError($"类型转换失败:无法将 {item.GetType().Name} 转换为 {typeof(T).Name}");
return null;
}
}
return result.ToArray();
}
catch (Exception ex)
{
Debug.LogError($"类型转换失败:从 Define 转换为 {typeof(T).Name} 时出错。错误信息:{ex.Message}");
return null;
}
}
public override string ToString()
{
if (packs == null || packs.Count == 0)