(client) chore:修改了角色的身体结构的定义方式,现在图片资源统一使用ImageDef加载,使用了更节省资源的初始化方式;fix:修复了定义加载数组时只能初始化数组而不能初始化列表的问题

This commit is contained in:
m0_75251201
2025-08-22 20:43:55 +08:00
parent 3e099137a1
commit 8916440e7e
28 changed files with 1411 additions and 954 deletions

View File

@ -15,192 +15,265 @@ namespace Managers
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();
public void Init()
{
if (packagesImages.Count > 0)
return;
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
InitImageDef();
InitDrawOrder();
}
public void InitImageDef()
{
var textureCache = new Dictionary<string, Texture2D>();
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)
if (string.IsNullOrEmpty(ima.path) || string.IsNullOrEmpty(ima.packID))
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);
}
}
public void InitDrawOrder()
{
try
{
// 查询绘制顺序定义
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
if (drawOrderDef == null || drawOrderDef.Length == 0)
try
{
Debug.LogWarning("No DrawingOrderDef found.");
return;
}
// 初始化包路径字典
Dictionary<string, string> packRootSite = new();
foreach (var drawOrder in drawOrderDef)
{
// 检查必要字段是否为空
if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
string cacheKey;
Texture2D texture;
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}");
continue;
// 去掉扩展名
var cleanPath = Path.ChangeExtension(resPath, null);
texture = Resources.Load<Texture2D>(cleanPath);
if (texture)
textureCache[cacheKey] = texture;
}
packRootSite[drawOrder.packID] = packagePath;
}
// 判断是否为 Unity 资源路径
var isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
var rootPath = packRootSite[drawOrder.packID];
if (isUnityResource)
else if (ima.path.Contains(':'))
{
// 移除 "res:" 前缀并适配 Unity 资源路径规则
var resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
// 处理其他包ID前缀如 "PackageID:Path"
var splitIndex = ima.path.IndexOf(':');
var packageID = ima.path.Substring(0, splitIndex);
var relativePath = ima.path.Substring(splitIndex + 1);
// 加载文件夹下的所有纹理资源
var textures = Resources.LoadAll<Texture2D>(resourceFolder);
if (textures == null || textures.Length == 0)
{
Debug.LogWarning($"No textures found in Unity resource folder: {resourceFolder}");
// 获取包根路径
var packageRoot = Managers.DefineManager.Instance.GetPackagePath(packageID);
if (string.IsNullOrEmpty(packageRoot))
continue;
}
foreach (var image in textures)
var fullPath = Path.Combine(packageRoot, relativePath).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower();
// 检查缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
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);
// 无前缀:使用当前定义所在包的路径
var pack = Managers.DefineManager.Instance.GetDefinePackage(ima);
var fullPath = Path.Combine(pack.packRootPath, ima.path).Replace('\\', '/');
cacheKey = "file://" + fullPath.ToLower();
// 获取图像文件列表
try
// 检查缓存
if (!textureCache.TryGetValue(cacheKey, out texture))
{
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}");
}
}
}
catch (Exception ex)
{
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($"Failed to load texture: {ima.path} (PackID: {ima.packID}, Name: {ima.name})");
continue;
}
// 存储到包资源字典使用原始packID非路径中的包ID
var packId = ima.packID;
if (!packagesImages.ContainsKey(packId))
packagesImages[packId] = new Dictionary<string, Texture2D>();
// 避免同一包内重复添加(查重)
if (!packagesImages[packId].ContainsKey(ima.name))
packagesImages[packId].Add(ima.name, texture);
else
packagesImages[packId][ima.name] = texture; // 覆盖已存在的引用
// 切分精灵
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
}
catch (Exception ex)
{
// 捕获异常并打印详细错误信息
Debug.LogError(
$"Error processing image definition: {ima.name} (Path: {ima.path}, PackID: {ima.packID}). Exception: {ex.Message}");
}
}
catch (Exception ex)
{
Debug.LogError($"An unexpected error occurred in InitDrawOrder: {ex.Message}");
}
}
// public void InitDrawOrder()
// {
// try
// {
// // 查询绘制顺序定义
// var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
// if (drawOrderDef == null || drawOrderDef.Length == 0)
// {
// Debug.LogWarning("No DrawingOrderDef found.");
// return;
// }
//
// // 初始化包路径字典
// Dictionary<string, string> packRootSite = new();
//
// foreach (var drawOrder in drawOrderDef)
// {
// // 检查必要字段是否为空
// if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
// {
// Debug.LogWarning(
// $"Skipping invalid drawOrder: texturePath or packID is null or empty. PackID: {drawOrder.packID}");
// continue;
// }
//
// // 获取包路径
// if (!packRootSite.ContainsKey(drawOrder.packID))
// {
// var packagePath = Managers.DefineManager.Instance.GetPackagePath(drawOrder.packID);
// if (string.IsNullOrEmpty(packagePath))
// {
// Debug.LogError($"Package path not found for packID: {drawOrder.packID}");
// continue;
// }
//
// packRootSite[drawOrder.packID] = packagePath;
// }
//
// // 判断是否为 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)
// {
// 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}");
// }
// }
// }
// else
// {
// // 文件系统路径处理
// var folderPath = Path.Combine(rootPath, drawOrder.texturePath);
//
// // 获取图像文件列表
// try
// {
// 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}");
// }
// }
// }
// catch (Exception ex)
// {
// Debug.LogError($"Failed to retrieve files from folder: {folderPath}. Error: {ex.Message}");
// }
// }
// }
// }
// catch (Exception ex)
// {
// Debug.LogError($"An unexpected error occurred in InitDrawOrder: {ex.Message}");
// }
// }
private void SplitTextureIntoSprites(
string packId,
@ -210,7 +283,7 @@ namespace Managers
int cols,
int pixelsPerUnit)
{
if (texture == null)
if (!texture)
{
Debug.LogError("Texture is null.");
return;
@ -256,7 +329,7 @@ namespace Managers
var index = (rows - row - 1) * cols + col;
var spriteName = $"{baseName}_{index}";
sprite.name = spriteName;
sprites[packId][spriteName] = sprite;
}
}
@ -297,96 +370,96 @@ namespace Managers
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();
}
// /// <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();
// }
}
}