(client) feat:实现热重载,实现多维度,实现武器,实现掉落物,实现状态UI,实现攻击AI (#44)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-08-27 19:56:49 +08:00
committed by TheRedApricot
parent d91210a6ff
commit 8456b6c162
132 changed files with 18568 additions and 2534 deletions

View File

@ -3,205 +3,199 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Data;
using NUnit.Framework;
using UnityEngine;
namespace Managers
{
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
/// <summary>
/// 包图像管理器,负责加载、管理和提供从定义数据中解析出的纹理和精灵。
/// </summary>
/// <remarks>
/// 该管理器是一个单例,并在启动过程中实现 ILaunchManager 接口,
/// 用于处理游戏或应用启动时图像资源的加载和初始化。
/// </remarks>
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>, ILaunchManager
{
/// <summary>
/// 默认精灵,在找不到对应图像时返回。
/// </summary>
public Sprite defaultSprite;
//包名,图片名
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
//包名,图片名
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
//包名,文件路径,身体部件名
public Dictionary<string, Dictionary<string, Dictionary<string, Sprite>>> bodyTexture = new();
/// <summary>
/// 存储所有已加载的纹理按包ID和图像名称索引。
/// </summary>
/// <remarks>
/// 外层字典键是包ID内层字典键是图像名称。
/// </remarks>
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
/// <summary>
/// 存储所有已创建的精灵按包ID和精灵名称索引。
/// </summary>
/// <remarks>
/// 外层字典键是包ID内层字典键是精灵名称如果纹理被分割会包含索引后缀
/// </remarks>
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
/// <summary>
/// 获取当前启动步骤的描述。
/// </summary>
public string StepDescription { get; private set; } = "包图像管理器正在准备中...";
/// <summary>
/// 初始化图像管理器,加载默认精灵并处理所有 ImageDef 定义。
/// </summary>
public void Init()
{
if (packagesImages.Count > 0)
return;
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
InitImageDef();
InitDrawOrder();
}
public void InitImageDef()
{
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
foreach (var ima in imageDef)
{
if (ima.path == null || ima.packID == null)
continue;
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
var path = Path.Combine(pack.packRootPath, ima.path);
var texture = Configs.ConfigProcessor.LoadTextureByIO(path);
if (texture == null)
continue;
var packId = ima.packID;
if (!packagesImages.ContainsKey(packId))
packagesImages[packId] = new Dictionary<string, Texture2D>();
packagesImages[packId].Add(ima.name, texture);
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
// 如果已经加载过,直接返回。
StepDescription = "包图像管理器已初始化。";
return;
}
StepDescription = "正在加载默认精灵..."; // 更新加载步骤描述
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
if (defaultSprite == null)
{
Debug.LogWarning("无法加载默认精灵 'Resources/Default/DefaultImage'。请确保文件存在。");
}
StepDescription = "正在处理图像定义并创建精灵..."; // 更新加载步骤描述
InitImageDef();
StepDescription = "包图像管理器初始化完成。"; // 完成加载
}
public void InitDrawOrder()
/// <summary>
/// 根据 ImageDef 定义初始化并加载所有纹理和精灵。
/// </summary>
public void InitImageDef()
{
try
var textureCache = new Dictionary<string, Texture2D>();
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
if (imageDef == null || !imageDef.Any())
{
// 查询绘制顺序定义
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
if (drawOrderDef == null || drawOrderDef.Length == 0)
Debug.Log($"在 DefineManager 中未找到任何 ImageDef 定义。({typeof(ImageDef).Name})");
return;
}
foreach (var ima in imageDef)
{
if (string.IsNullOrEmpty(ima.path) || string.IsNullOrEmpty(ima.packID))
{
Debug.LogWarning("No DrawingOrderDef found.");
return;
Debug.LogWarning($"跳过图像定义 '{ima?.name ?? ""}'因为它包含空路径或包ID。(路径: '{ima?.path ?? ""}', 包ID: '{ima?.packID ?? ""}')");
continue;
}
// 初始化包路径字典
Dictionary<string, string> packRootSite = new();
foreach (var drawOrder in drawOrderDef)
try
{
// 检查必要字段是否为空
if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
string cacheKey;
Texture2D texture = null;
if (ima.path.StartsWith("res:"))
{
Debug.LogWarning(
$"Skipping invalid drawOrder: texturePath or packID is null or empty. PackID: {drawOrder.packID}");
continue;
}
// 处理 Resources 路径
var resPath = ima.path.Substring(4).Replace('\\', '/').TrimStart('/');
cacheKey = "res://" + resPath.ToLower(); // 缓存键使用小写路径
// 获取包路径
if (!packRootSite.ContainsKey(drawOrder.packID))
{
var packagePath = Managers.DefineManager.Instance.GetPackagePath(drawOrder.packID);
if (string.IsNullOrEmpty(packagePath))
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
Debug.LogError($"Package path not found for packID: {drawOrder.packID}");
var cleanPath = Path.ChangeExtension(resPath, null); // 去掉扩展名
texture = Resources.Load<Texture2D>(cleanPath);
if (texture)
textureCache[cacheKey] = texture;
}
}
else if (ima.path.Contains(':'))
{
// 处理其他包ID前缀如 "PackageID:Path"
var splitIndex = ima.path.IndexOf(':');
var packageID = ima.path.Substring(0, splitIndex);
var relativePath = ima.path.Substring(splitIndex + 1);
// 获取包根路径
var packageRoot = Managers.DefineManager.Instance.GetPackagePath(packageID);
if (string.IsNullOrEmpty(packageRoot))
{
Debug.LogWarning($"图像定义 '{ima.name}' (包ID: {ima.packID}): 引用的包ID '{packageID}' 未找到或没有根路径。跳过图像加载。");
continue;
}
packRootSite[drawOrder.packID] = packagePath;
}
var fullPath = Path.Combine(packageRoot, relativePath).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower(); // 缓存键使用小写路径
// 判断是否为 Unity 资源路径
var isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
var rootPath = packRootSite[drawOrder.packID];
if (isUnityResource)
{
// 移除 "res:" 前缀并适配 Unity 资源路径规则
var resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
// 加载文件夹下的所有纹理资源
var textures = Resources.LoadAll<Texture2D>(resourceFolder);
if (textures == null || textures.Length == 0)
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
Debug.LogWarning($"No textures found in Unity resource folder: {resourceFolder}");
continue;
}
foreach (var image in textures)
{
if (image == null)
{
Debug.LogWarning(
$"Texture loaded from Unity resource folder: {resourceFolder} is null.");
continue;
}
// 创建精灵
try
{
var spr = Sprite.Create(
image,
new Rect(0, 0, image.width, image.height),
new Vector2(0.5f, 0.5f), // 中心点
drawOrder.pixelsPerUnit
);
var name = image.name;
// 插入纹理
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
}
catch (Exception ex)
{
Debug.LogError(
$"Failed to create sprite from Unity resource: {image.name}. Error: {ex.Message}");
}
texture = Configs.ConfigProcessor.LoadTextureByIO(fullPath);
if (texture)
textureCache[cacheKey] = texture;
}
}
else
{
// 文件系统路径处理
var folderPath = Path.Combine(rootPath, drawOrder.texturePath);
// 获取图像文件列表
try
// 无前缀:使用当前定义所在包的路径
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
if (pack == null)
{
var imagePath = Configs.ConfigProcessor.GetFilesByExtensions(folderPath,
new[] { "jpg", "jpeg", "png", "tga", "tif", "tiff", "psd", "bmp" });
foreach (var path in imagePath)
{
// 加载纹理
Texture2D image = null;
try
{
image = Configs.ConfigProcessor.LoadTextureByIO(path);
}
catch (Exception ex)
{
Debug.LogError($"Failed to load texture from path: {path}. Error: {ex.Message}");
continue;
}
if (image == null)
{
Debug.LogWarning($"Texture loaded from path: {path} is null.");
continue;
}
// 创建精灵
try
{
var spr = Sprite.Create(
image,
new Rect(0, 0, image.width, image.height),
new Vector2(0.5f, 0.5f), // 中心点
drawOrder.pixelsPerUnit
);
var name = Path.GetFileNameWithoutExtension(path);
// 插入纹理
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
}
catch (Exception ex)
{
Debug.LogError(
$"Failed to create sprite from texture: {path}. Error: {ex.Message}");
}
}
Debug.LogError($"图像定义 '{ima.name}' (包ID: {ima.packID}): 源图像未找到对应的定义包。无法确定 '{ima.path}' 的完整路径。跳过。");
continue;
}
catch (Exception ex)
var fullPath = Path.Combine(pack.packRootPath, ima.path).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower(); // 缓存键使用小写路径
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
Debug.LogError($"Failed to retrieve files from folder: {folderPath}. Error: {ex.Message}");
texture = Configs.ConfigProcessor.LoadTextureByIO(fullPath);
if (texture)
textureCache[cacheKey] = texture;
}
}
// 资源加载失败
if (!texture)
{
Debug.LogError($"未能加载图像定义所在的纹理: '{ima.name}' (路径: '{ima.path}', 包ID: '{ima.packID}')。请验证路径和文件是否存在。");
continue;
}
// 存储到包纹理字典(使用定义自身的 packID
var packId = ima.packID;
if (!packagesImages.ContainsKey(packId))
packagesImages[packId] = new Dictionary<string, Texture2D>();
// 警告:如果图片名重复,则覆盖
if (packagesImages[packId].ContainsKey(ima.name))
{
Debug.LogWarning($"包 '{packId}' 中名为 '{ima.name}' 的图像被多次定义。将覆盖之前的纹理引用。这可能表示配置错误。");
}
packagesImages[packId][ima.name] = texture; // 覆盖或添加
// 切分精灵
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
}
catch (Exception ex)
{
// 捕获异常并打印详细错误信息
Debug.LogError(
$"处理图像定义时出错: '{ima.name}' (路径: '{ima.path}', 包ID: '{ima.packID}')。异常: {ex.GetType().Name}: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
}
}
catch (Exception ex)
{
Debug.LogError($"An unexpected error occurred in InitDrawOrder: {ex.Message}");
}
}
/// <summary>
/// 将纹理按指定行数和列数分割成多个精灵,并存储起来。
/// </summary>
/// <param name="packId">精灵所属的包ID。</param>
/// <param name="baseName">精灵的基础名称。</param>
/// <param name="texture">要分割的 <see cref="Texture2D"/> 对象。</param>
/// <param name="rows">水平分割的行数。</param>
/// <param name="cols">垂直分割的列数。</param>
/// <param name="pixelsPerUnit">每个单元的像素数用于Sprite.Create。</param>
private void SplitTextureIntoSprites(
string packId,
string baseName,
@ -210,9 +204,9 @@ namespace Managers
int cols,
int pixelsPerUnit)
{
if (texture == null)
if (!texture)
{
Debug.LogError("Texture is null.");
Debug.LogError($"SplitTextureIntoSprites: 包 '{packId}' 中 '{baseName}' 提供的纹理为空。无法分割。");
return;
}
@ -223,60 +217,107 @@ namespace Managers
var textureWidth = texture.width;
var textureHeight = texture.height;
// 如果不分割rows和cols都为1直接创建单个Sprite
if (rows == 1 && cols == 1)
{
if (!sprites.ContainsKey(packId))
sprites[packId] = new Dictionary<string, Sprite>();
var spriteRect = new Rect(0, 0, textureWidth, textureHeight);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
sprites[packId][baseName] = sprite;
return;
}
var tileWidth = textureWidth / cols;
var tileHeight = textureHeight / rows;
if (tileWidth * cols != textureWidth || tileHeight * rows != textureHeight)
{
Debug.LogError("Texture dimensions are not divisible by the specified rows and columns.");
return;
}
if (!sprites.ContainsKey(packId))
sprites[packId] = new Dictionary<string, Sprite>();
// 创建未分割的完整精灵,使用原始名称
var fullSpriteRect = new Rect(0, 0, textureWidth, textureHeight);
// 警告:如果精灵名重复,则覆盖
if (sprites[packId].ContainsKey(baseName))
{
Debug.LogWarning($"包 '{packId}' 中名为 '{baseName}' 的精灵已存在。将覆盖之前的完整精灵定义。");
}
var fullSprite = Sprite.Create(texture, fullSpriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
fullSprite.name = baseName; // 确保 Sprite.name 被设置
sprites[packId][baseName] = fullSprite;
// 如果不分割rows和cols都为1提前返回
if (rows == 1 && cols == 1)
{
return;
}
// 检查纹理尺寸是否可被分割数整除
if (textureWidth % cols != 0 || textureHeight % rows != 0)
{
Debug.LogError($"包 '{packId}' 中 '{baseName}' 的纹理尺寸 ({textureWidth}x{textureHeight}) 不能被指定的行数 ({rows}) 和列数 ({cols}) 完美整除。子精灵将不会生成或可能不正确。仅显示完整精灵。");
return; // 终止子精灵分割,只保留完整的精灵
}
var tileWidth = textureWidth / cols;
var tileHeight = textureHeight / rows;
for (var row = 0; row < rows; row++)
{
for (var col = 0; col < cols; col++)
{
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
var index = (rows - row - 1) * cols + col;
var spriteName = $"{baseName}_{index}";
// 精灵索引计算方式
var index = (rows - row - 1) * cols + col;
var spriteName = $"{baseName}_{index}";
sprite.name = spriteName;
// 警告:如果子精灵名重复,则覆盖
if (sprites[packId].ContainsKey(spriteName))
{
Debug.LogWarning($"包 '{packId}' 中名为 '{spriteName}' 的精灵已存在。将覆盖之前的子精灵定义。这可能表示配置错误。");
}
sprites[packId][spriteName] = sprite;
}
}
}
public void Reload()
/// <summary>
/// 清理所有已加载的纹理和精灵数据。
/// </summary>
/// <remarks>
/// 此方法会清空 <see cref="packagesImages"/> 和 <see cref="sprites"/> 字典,
/// 但不会卸载 <see cref="defaultSprite"/>,因为它通常通过 Resources.Load 加载,由 Unity 管理其生命周期。
/// </remarks>
public void Clear()
{
packagesImages.Clear();
sprites.Clear();
Init();
StepDescription = "包图像管理器数据已清理。"; // 更新状态
}
/// <summary>
/// 重新加载所有图像数据。
/// </summary>
/// <remarks>
/// 此方法会首先调用 <see cref="Clear()"/> 清理所有数据,然后调用 <see cref="Init()"/> 重新初始化。
/// </remarks>
public void Reload()
{
Clear();
Init();
}
/// <summary>
/// 根据 <see cref="ImageDef"/> 对象获取对应的精灵。
/// </summary>
/// <param name="ima">包含精灵包ID和名称的 <see cref="ImageDef"/> 对象。</param>
/// <returns>如果找到对应的精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
public Sprite GetSprite(ImageDef ima)
{
return GetSprite(ima.packID,ima.name);
if (ima == null) return defaultSprite;
return GetSprite(ima.packID, ima.name);
}
/// <summary>
/// 根据包ID和精灵名称获取对应的精灵。
/// </summary>
/// <param name="packID">精灵所属的包ID。如果为空则会遍历所有包查找。</param>
/// <param name="name">精灵的名称。</param>
/// <returns>如果找到对应的精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
public Sprite GetSprite(string packID, string name)
{
if (string.IsNullOrEmpty(packID))
{
// 如果packID为空遍历所有包以name查找
foreach (var kvp in sprites)
{
if (kvp.Value.TryGetValue(name, out var sprite))
@ -289,104 +330,21 @@ namespace Managers
return sprite;
}
// 如果未找到,返回默认精灵
return defaultSprite;
}
/// <summary>
/// 根据包ID、基础名称和索引获取被分割的子精灵。
/// </summary>
/// <param name="packID">精灵所属的包ID。</param>
/// <param name="name">精灵的基础名称。</param>
/// <param name="index">子精灵的索引。</param>
/// <returns>如果找到对应的子精灵,则返回该精灵;否则返回 <see cref="defaultSprite"/>。</returns>
public Sprite GetSprite(string packID, string name, int index)
{
var fullName = $"{name}_{index}";
return GetSprite(packID, fullName);
}
/// <summary>
/// 向 bodyTexture 插入一张 Sprite。
/// 如果包名、路径或部件名原本不存在,会自动建立对应的 Dictionary。
/// </summary>
/// <param name="packageName">包名</param>
/// <param name="filePath">文件路径</param>
/// <param name="bodyPartName">身体部件名</param>
/// <param name="sprite">要插入的 Sprite</param>
public void InsertBodyTexture(string packageName,
string filePath,
string bodyPartName,
Sprite sprite)
{
if (sprite == null)
{
Debug.LogWarning("InsertBodyTexture: sprite 为 null已忽略。");
return;
}
// 1) 处理包名层级
if (!bodyTexture.TryGetValue(packageName, out var pathDict))
{
pathDict = new Dictionary<string, Dictionary<string, Sprite>>();
bodyTexture[packageName] = pathDict;
}
// 2) 处理文件路径层级
if (!pathDict.TryGetValue(filePath, out var partDict))
{
partDict = new Dictionary<string, Sprite>();
pathDict[filePath] = partDict;
}
// 3) 插入或覆盖部件名
partDict[bodyPartName] = sprite;
}
/// <summary>
/// 查找身体部件的所有Sprite变体支持带编号的图片
/// </summary>
/// <param name="packageName">包名</param>
/// <param name="filePath">文件路径</param>
/// <param name="bodyPartName">身体部件名</param>
/// <returns>按编号排序的Sprite数组未找到时返回空数组</returns>
public Sprite[] FindBodyTextures(string packageName, string filePath, string bodyPartName)
{
// 检查包名是否存在
if (!bodyTexture.TryGetValue(packageName, out var packageDict))
{
Debug.LogWarning($"Package '{packageName}' not found.");
return new[] { defaultSprite };
}
// 检查文件路径是否存在
if (!packageDict.TryGetValue(filePath, out var pathDict))
{
Debug.LogWarning($"File path '{filePath}' not found in package '{packageName}'.");
return new[] { defaultSprite };
}
// 收集所有匹配的Sprite
var sprites = new List<(int order, Sprite sprite)>();
// 查找完全匹配的项(无编号)
if (pathDict.TryGetValue(bodyPartName, out var baseSprite))
{
sprites.Add((0, baseSprite));
}
// 查找带编号的变体
var prefix = bodyPartName + "_";
foreach (var (key, value) in pathDict)
{
// 检查是否以部件名+下划线开头
if (key.StartsWith(prefix) && key.Length > prefix.Length)
{
var suffix = key.Substring(prefix.Length);
// 尝试解析编号
if (int.TryParse(suffix, out var number))
{
sprites.Add((number, value));
}
}
}
// 按编号排序无编号视为0
return sprites
.OrderBy(x => x.order)
.Select(x => x.sprite)
.ToArray();
}
}
}
}