using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Data;
using UnityEngine;
namespace Managers
{
///
/// 包图像管理器,负责加载、管理和提供从定义数据中解析出的纹理和精灵。
///
///
/// 该管理器是一个单例,并在启动过程中实现 ILaunchManager 接口,
/// 用于处理游戏或应用启动时图像资源的加载和初始化。
///
public class PackagesImageManager : Utils.Singleton, ILaunchManager
{
///
/// 默认精灵,在找不到对应图像时返回。
///
public Sprite defaultSprite;
///
/// 存储所有已加载的纹理,按图像名称(全局唯一的DefName)索引。
///
public Dictionary packagesImages = new();
///
/// 存储所有已创建的精灵,按精灵名称(全局唯一的DefName或其带索引后缀)索引。
///
public Dictionary sprites = new();
///
/// 获取当前启动步骤的描述。
///
public string StepDescription { get; private set; } = "包图像管理器正在准备中...";
///
/// 初始化图像管理器,加载默认精灵并处理所有 ImageDef 定义。
///
public void Init()
{
if (packagesImages.Count > 0)
{
// 如果已经有数据,则跳过初始化,防止重复加载。
return;
}
defaultSprite = Resources.Load("Default/DefaultImage");
if (defaultSprite == null)
{
Debug.LogWarning("无法加载默认精灵 'Resources/Default/DefaultImage'。请确保文件存在。");
}
InitImageDef();
}
///
/// 根据 ImageDef 定义初始化并加载所有纹理和精灵。
///
public void InitImageDef()
{
var textureCache = new Dictionary();
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType();
if (imageDef == null || !imageDef.Any())
{
Debug.Log($"在定义管理器中未找到任何图像定义。({nameof(ImageDef)})");
return;
}
foreach (var ima in imageDef)
{
if (string.IsNullOrEmpty(ima.path) || string.IsNullOrEmpty(ima.packID))
{
Debug.LogWarning($"跳过图像定义 '{ima?.defName ?? "未知"}',因为它包含空路径或包ID。(路径: '{ima?.path ?? ""}', 包ID: '{ima?.packID ?? ""}')");
continue;
}
try
{
string cacheKey;
Texture2D texture = null;
if (ima.path.StartsWith("res:"))
{
// 处理 Resources 路径
var resPath = ima.path.Substring(4).Replace('\\', '/').TrimStart('/');
cacheKey = "res://" + resPath.ToLower(); // 缓存键使用小写路径
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
var cleanPath = Path.ChangeExtension(resPath, null); // 去掉扩展名
texture = Resources.Load(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.defName}' (包ID: {ima.packID}): 引用的包ID '{packageID}' 未找到或没有根路径。跳过图像加载。");
continue;
}
var fullPath = Path.Combine(packageRoot, relativePath).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower(); // 缓存键使用小写路径
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
texture = Configs.ConfigProcessor.LoadTextureByIO(fullPath);
if (texture)
textureCache[cacheKey] = texture;
}
}
else
{
// 无前缀:使用当前定义所在包的路径
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
if (pack == null)
{
Debug.LogError($"图像定义 '{ima.defName}' (包ID: {ima.packID}): 源图像未找到对应的定义包。无法确定 '{ima.path}' 的完整路径。跳过。");
continue;
}
var fullPath = Path.Combine(pack.packRootPath, ima.path).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower(); // 缓存键使用小写路径
// 检查纹理缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
texture = Configs.ConfigProcessor.LoadTextureByIO(fullPath);
if (texture)
textureCache[cacheKey] = texture;
}
}
// 资源加载失败
if (!texture)
{
Debug.LogError($"未能加载图像定义关联的纹理: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}')。请验证路径和文件是否存在。");
continue;
}
packagesImages[ima.defName] = texture;
// 切分精灵
SplitTextureIntoSprites(ima.defName, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
}
catch (Exception ex)
{
// 捕获异常并打印详细错误信息
Debug.LogError(
$"处理图像定义时出错: '{ima.defName}' (路径: '{ima.path}', 包ID: '{ima.packID}')。异常: {ex.GetType().Name}: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
}
}
}
///
/// 将纹理按指定行数和列数分割成多个精灵,并存储起来。
///
/// 精灵的基础名称(全局唯一的DefName)。
/// 要分割的 对象。
/// 水平分割的行数。
/// 垂直分割的列数。
/// 每个单元的像素数,用于Sprite.Create。
private void SplitTextureIntoSprites(
string baseName,
Texture2D texture,
int rows,
int cols,
int pixelsPerUnit)
{
if (!texture)
{
Debug.LogError($"SplitTextureIntoSprites: '{baseName}' 提供的纹理为空。无法分割。");
return;
}
// 如果行数或列数小于1,则设为1(不分割)
rows = Mathf.Max(1, rows);
cols = Mathf.Max(1, cols);
var textureWidth = texture.width;
var textureHeight = texture.height;
// 创建未分割的完整精灵,使用原始名称 (baseName,即 ImageDef.name)
var fullSpriteRect = new Rect(0, 0, textureWidth, textureHeight);
var fullSprite = Sprite.Create(texture, fullSpriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
fullSprite.name = baseName; // 确保 Sprite.name 被设置
sprites[baseName] = fullSprite;
// 如果不分割(rows和cols都为1),提前返回
if (rows == 1 && cols == 1)
{
return;
}
// 检查纹理尺寸是否可被分割数整除
if (textureWidth % cols != 0 || textureHeight % rows != 0)
{
Debug.LogError($"'{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);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
// 精灵索引计算方式
var index = (rows - row - 1) * cols + col;
var spriteName = $"{baseName}_{index}";
sprite.name = spriteName;
sprites[spriteName] = sprite;
}
}
}
///
/// 清理所有已加载的纹理和精灵数据。
///
///
/// 此方法会清空 和 字典,
/// 但不会卸载 ,因为它通常通过 Resources.Load 加载,由 Unity 管理其生命周期。
///
public void Clear()
{
packagesImages.Clear();
sprites.Clear();
StepDescription = "包图像管理器数据已清理。"; // 更新状态
}
///
/// 重新加载所有图像数据。
///
///
/// 此方法会首先调用 清理所有数据,然后调用 重新初始化。
///
public void Reload()
{
Clear();
Init();
}
///
/// 根据 对象获取对应的精灵。
///
/// 包含精灵名称的 对象。
/// 如果找到对应的精灵,则返回该精灵;否则返回 。
public Sprite GetSprite(ImageDef ima)
{
if (ima == null) return defaultSprite;
return GetSprite(ima.packID, ima.defName);
}
///
/// 根据包ID和精灵名称获取对应的精灵。
///
/// 精灵所属的包ID。此参数在此版本中已不再用于字典查找,但为保持兼容性而保留。
/// 精灵的名称(全局唯一的DefName)。
/// 如果找到对应的精灵,则返回该精灵;否则返回 。
public Sprite GetSprite(string packID, string name)
{
if (sprites.TryGetValue(name, out var sprite))
return sprite;
// 如果未找到,返回默认精灵
return defaultSprite;
}
///
/// 根据包ID、基础名称和索引获取被分割的子精灵。
///
/// 精灵所属的包ID。此参数在此版本中已不再用于字典查找,但为保持兼容性而保留。
/// 精灵的基础名称(全局唯一的DefName)。
/// 子精灵的索引。
/// 如果找到对应的子精灵,则返回该精灵;否则返回 。
public Sprite GetSprite(string packID, string name, int index)
{
var fullName = $"{name}_{index}";
return GetSprite(packID, fullName);
}
// ---------- 新增的查询接口 ----------
///
/// 根据精灵名称(全局唯一的DefName)获取对应的精灵。
///
/// 精灵的名称。
/// 如果找到对应的精灵,则返回该精灵;否则返回 。
public Sprite GetSprite(string name)
{
if (sprites.TryGetValue(name, out var sprite))
return sprite;
return defaultSprite;
}
///
/// 根据基础名称(全局唯一的DefName)和索引获取被分割的子精灵。
///
/// 精灵的基础名称。
/// 子精灵的索引。
/// 如果找到对应的子精灵,则返回该精灵;否则返回 。
public Sprite GetSprite(string name, int index)
{
var fullName = $"{name}_{index}";
return GetSprite(fullName);
}
}
}