2025-07-24 23:19:48 +08:00
|
|
|
|
using System;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2025-07-25 17:27:08 +08:00
|
|
|
|
using System.Linq;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
using Data;
|
2025-07-24 23:19:48 +08:00
|
|
|
|
using NUnit.Framework;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Managers
|
|
|
|
|
{
|
|
|
|
|
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
|
|
|
|
|
{
|
2025-07-23 22:54:00 +08:00
|
|
|
|
public Sprite defaultSprite;
|
2025-07-24 23:19:48 +08:00
|
|
|
|
//包名,图片名
|
2025-07-21 16:17:12 +08:00
|
|
|
|
public Dictionary<string, Dictionary<string, Texture2D>> packagesImages = new();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
//包名,图片名
|
2025-07-21 16:17:12 +08:00
|
|
|
|
public Dictionary<string, Dictionary<string, Sprite>> sprites = new();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
//包名,文件路径,身体部件名
|
2025-07-25 17:27:08 +08:00
|
|
|
|
public Dictionary<string, Dictionary<string, Dictionary<string, Sprite>>> bodyTexture = new();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
|
2025-07-17 15:42:24 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
if (packagesImages.Count > 0)
|
|
|
|
|
return;
|
2025-07-23 22:54:00 +08:00
|
|
|
|
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
|
2025-07-24 23:19:48 +08:00
|
|
|
|
InitImageDef();
|
|
|
|
|
InitDrawOrder();
|
|
|
|
|
packagesImages = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitImageDef()
|
|
|
|
|
{
|
2025-07-17 15:42:24 +08:00
|
|
|
|
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
|
|
|
|
|
foreach (var ima in imageDef)
|
|
|
|
|
{
|
2025-07-21 16:17:12 +08:00
|
|
|
|
if (ima.path == null || ima.packID == null)
|
2025-07-17 15:42:24 +08:00
|
|
|
|
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)
|
2025-07-21 16:17:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var packId = ima.packID;
|
|
|
|
|
|
|
|
|
|
if (!packagesImages.ContainsKey(packId))
|
|
|
|
|
packagesImages[packId] = new Dictionary<string, Texture2D>();
|
|
|
|
|
packagesImages[packId].Add(ima.name, texture);
|
2025-07-22 14:40:24 +08:00
|
|
|
|
|
2025-07-21 16:17:12 +08:00
|
|
|
|
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
2025-07-24 23:19:48 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitDrawOrder()
|
|
|
|
|
{
|
|
|
|
|
var drawOrderDef = Managers.DefineManager.Instance.QueryDefinesByType<DrawingOrderDef>();
|
|
|
|
|
if (drawOrderDef == null || drawOrderDef.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Dictionary<string, string> packRootSite = new();
|
|
|
|
|
foreach (var drawOrder in drawOrderDef)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(drawOrder.texturePath) || string.IsNullOrEmpty(drawOrder.packID))
|
|
|
|
|
continue;
|
|
|
|
|
if (!packRootSite.ContainsKey(drawOrder.packID))
|
|
|
|
|
packRootSite[drawOrder.packID] = Managers.DefineManager.Instance.GetPackagePath(drawOrder.packID);
|
|
|
|
|
var rootPath= packRootSite[drawOrder.packID];
|
|
|
|
|
var folderPath=Path.Combine(rootPath, drawOrder.texturePath);
|
|
|
|
|
var imagePath = Configs.ConfigProcessor.GetFilesByExtensions(folderPath,
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
"jpg", "jpeg", "png", "tga", "tif", "tiff", "psd", "bmp"
|
|
|
|
|
});
|
|
|
|
|
foreach (var path in imagePath)
|
|
|
|
|
{
|
|
|
|
|
var image=Configs.ConfigProcessor.LoadTextureByIO(path);
|
|
|
|
|
if (image == null)
|
|
|
|
|
continue;
|
|
|
|
|
var spr=Sprite.Create(
|
|
|
|
|
image,
|
|
|
|
|
new Rect(0, 0, image.width, image.height),
|
|
|
|
|
new Vector2(0.5f, 0.5f) // 中心点
|
|
|
|
|
);
|
|
|
|
|
var name=Path.GetFileNameWithoutExtension(path);
|
2025-07-25 17:27:08 +08:00
|
|
|
|
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
|
|
|
|
|
|
2025-07-24 23:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 16:17:12 +08:00
|
|
|
|
private void SplitTextureIntoSprites(
|
|
|
|
|
string packId,
|
|
|
|
|
string baseName,
|
|
|
|
|
Texture2D texture,
|
|
|
|
|
int rows,
|
|
|
|
|
int cols,
|
|
|
|
|
int pixelsPerUnit)
|
2025-07-17 15:42:24 +08:00
|
|
|
|
{
|
2025-07-22 14:40:24 +08:00
|
|
|
|
if (texture == null)
|
2025-07-17 15:42:24 +08:00
|
|
|
|
{
|
2025-07-22 14:40:24 +08:00
|
|
|
|
Debug.LogError("Texture is null.");
|
2025-07-17 15:42:24 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 14:40:24 +08:00
|
|
|
|
// 如果行数或列数小于1,则设为1(不分割)
|
|
|
|
|
rows = Mathf.Max(1, rows);
|
|
|
|
|
cols = Mathf.Max(1, cols);
|
|
|
|
|
|
2025-07-17 15:42:24 +08:00
|
|
|
|
var textureWidth = texture.width;
|
|
|
|
|
var textureHeight = texture.height;
|
|
|
|
|
|
2025-07-22 14:40:24 +08:00
|
|
|
|
// 如果不分割(rows和cols都为1),直接创建单个Sprite
|
|
|
|
|
if (rows == 1 && cols == 1)
|
|
|
|
|
{
|
|
|
|
|
if (!sprites.ContainsKey(packId))
|
|
|
|
|
sprites[packId] = new Dictionary<string, Sprite>();
|
|
|
|
|
|
2025-07-25 17:27:08 +08:00
|
|
|
|
var spriteRect = new Rect(0, 0, textureWidth, textureHeight);
|
2025-07-22 14:40:24 +08:00
|
|
|
|
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
|
|
|
|
sprites[packId][baseName] = sprite;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 15:42:24 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2025-07-22 14:40:24 +08:00
|
|
|
|
|
2025-07-21 16:17:12 +08:00
|
|
|
|
if (!sprites.ContainsKey(packId))
|
|
|
|
|
sprites[packId] = new Dictionary<string, Sprite>();
|
2025-07-17 15:42:24 +08:00
|
|
|
|
|
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
|
{
|
|
|
|
|
for (var col = 0; col < cols; col++)
|
|
|
|
|
{
|
2025-07-21 16:17:12 +08:00
|
|
|
|
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
|
|
|
|
|
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
2025-07-22 14:40:24 +08:00
|
|
|
|
|
2025-07-19 19:03:53 +08:00
|
|
|
|
var index = (rows - row - 1) * cols + col;
|
2025-07-21 16:17:12 +08:00
|
|
|
|
var spriteName = $"{baseName}_{index}";
|
|
|
|
|
|
|
|
|
|
sprites[packId][spriteName] = sprite;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reload()
|
|
|
|
|
{
|
|
|
|
|
packagesImages.Clear();
|
2025-07-21 16:17:12 +08:00
|
|
|
|
sprites.Clear();
|
2025-07-17 15:42:24 +08:00
|
|
|
|
Init();
|
|
|
|
|
}
|
2025-07-21 16:17:12 +08:00
|
|
|
|
|
|
|
|
|
public Sprite GetSprite(string packID, string name)
|
2025-07-17 15:42:24 +08:00
|
|
|
|
{
|
2025-07-21 16:17:12 +08:00
|
|
|
|
if (string.IsNullOrEmpty(packID))
|
|
|
|
|
{
|
|
|
|
|
foreach (var kvp in sprites)
|
|
|
|
|
{
|
|
|
|
|
if (kvp.Value.TryGetValue(name, out var sprite))
|
|
|
|
|
return sprite;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-23 22:54:00 +08:00
|
|
|
|
else if (sprites.TryGetValue(packID, out var dict))
|
2025-07-21 16:17:12 +08:00
|
|
|
|
{
|
2025-07-23 22:54:00 +08:00
|
|
|
|
if (dict.TryGetValue(name, out var sprite))
|
|
|
|
|
return sprite;
|
2025-07-21 16:17:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:54:00 +08:00
|
|
|
|
return defaultSprite;
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 16:17:12 +08:00
|
|
|
|
public Sprite GetSprite(string packID, string name, int index)
|
2025-07-17 15:42:24 +08:00
|
|
|
|
{
|
2025-07-21 16:17:12 +08:00
|
|
|
|
var fullName = $"{name}_{index}";
|
|
|
|
|
return GetSprite(packID, fullName);
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
2025-07-24 23:19:48 +08:00
|
|
|
|
/// <summary>
|
2025-07-25 17:27:08 +08:00
|
|
|
|
/// 向 bodyTexture 插入一张 Sprite。
|
|
|
|
|
/// 如果包名、路径或部件名原本不存在,会自动建立对应的 Dictionary。
|
2025-07-24 23:19:48 +08:00
|
|
|
|
/// </summary>
|
2025-07-25 17:27:08 +08:00
|
|
|
|
/// <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)
|
2025-07-24 23:19:48 +08:00
|
|
|
|
{
|
2025-07-25 17:27:08 +08:00
|
|
|
|
if (sprite == null)
|
2025-07-24 23:19:48 +08:00
|
|
|
|
{
|
2025-07-25 17:27:08 +08:00
|
|
|
|
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 Array.Empty<Sprite>();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:27:08 +08:00
|
|
|
|
// 检查文件路径是否存在
|
|
|
|
|
if (!packageDict.TryGetValue(filePath, out var pathDict))
|
2025-07-24 23:19:48 +08:00
|
|
|
|
{
|
2025-07-25 17:27:08 +08:00
|
|
|
|
Debug.LogWarning($"File path '{filePath}' not found in package '{packageName}'.");
|
|
|
|
|
return Array.Empty<Sprite>();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:27:08 +08:00
|
|
|
|
// 收集所有匹配的Sprite
|
|
|
|
|
var sprites = new List<(int order, Sprite sprite)>();
|
|
|
|
|
|
|
|
|
|
// 查找完全匹配的项(无编号)
|
|
|
|
|
if (pathDict.TryGetValue(bodyPartName, out var baseSprite))
|
2025-07-24 23:19:48 +08:00
|
|
|
|
{
|
2025-07-25 17:27:08 +08:00
|
|
|
|
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: value));
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 23:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:27:08 +08:00
|
|
|
|
// 按编号排序(无编号视为0)
|
|
|
|
|
return sprites
|
|
|
|
|
.OrderBy(x => x.order)
|
|
|
|
|
.Select(x => x.sprite)
|
|
|
|
|
.ToArray();
|
2025-07-24 23:19:48 +08:00
|
|
|
|
}
|
2025-07-17 15:42:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|