(client) feat:添加瓦片动态定义和资源动态加载
This commit is contained in:
@ -97,6 +97,129 @@ namespace Managers
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public DefinePack GetDefinePackage(Define define)
|
||||
{
|
||||
if (define == null || define.packID == null)
|
||||
return null;
|
||||
packs.TryGetValue(define.packID, out var pack);
|
||||
return pack;
|
||||
}
|
||||
|
||||
public string GetPackagePath(string packID)
|
||||
{
|
||||
if (packs.TryGetValue(packID, out var pack))
|
||||
{
|
||||
return pack.packRootPath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Define[] GetAllDefine()
|
||||
{
|
||||
List<Define> defineList = new();
|
||||
foreach (var define in defines.Values)
|
||||
{
|
||||
defineList.AddRange(define.Values);
|
||||
}
|
||||
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>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
public Define[] QueryDefinesByType(string defineType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
return typeDefinitions.Values.ToArray();
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryDefinesByType<T>()
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取所有值并尝试转换为目标类型
|
||||
var result = new List<T>();
|
||||
foreach (var item in typeDefinitions.Values)
|
||||
{
|
||||
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)
|
||||
|
Reference in New Issue
Block a user