Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Managers/PackagesImageManager.cs
2025-07-24 23:19:48 +08:00

213 lines
7.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using Data;
using NUnit.Framework;
using UnityEngine;
namespace Managers
{
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
{
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, List<Sprite>>>> bodyTexture = new();
public void Init()
{
if (packagesImages.Count > 0)
return;
defaultSprite = Resources.Load<Sprite>("Default/DefaultImage");
InitImageDef();
InitDrawOrder();
packagesImages = null;
}
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);
}
}
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);
InsertBodySprite(drawOrder.packID, drawOrder.texturePath, name, spr);
}
}
}
private void SplitTextureIntoSprites(
string packId,
string baseName,
Texture2D texture,
int rows,
int cols,
int pixelsPerUnit)
{
if (texture == null)
{
Debug.LogError("Texture is null.");
return;
}
// 如果行数或列数小于1则设为1不分割
rows = Mathf.Max(1, rows);
cols = Mathf.Max(1, cols);
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>();
Rect 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>();
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}";
sprites[packId][spriteName] = sprite;
}
}
}
public void Reload()
{
packagesImages.Clear();
sprites.Clear();
Init();
}
public Sprite GetSprite(string packID, string name)
{
if (string.IsNullOrEmpty(packID))
{
foreach (var kvp in sprites)
{
if (kvp.Value.TryGetValue(name, out var sprite))
return sprite;
}
}
else if (sprites.TryGetValue(packID, out var dict))
{
if (dict.TryGetValue(name, out var sprite))
return sprite;
}
return defaultSprite;
}
public Sprite GetSprite(string packID, string name, int index)
{
var fullName = $"{name}_{index}";
return GetSprite(packID, fullName);
}
/// <summary>
/// 插入 Sprite 到指定的嵌套字典中。
/// </summary>
/// <param name="packageName">包名。</param>
/// <param name="filePath">文件路径。</param>
/// <param name="bodyPartName">身体部件名。</param>
/// <param name="sprite">要插入的 Sprite 对象。</param>
public void InsertBodySprite(string packageName, string filePath, string bodyPartName, Sprite sprite)
{
// 确保第一层字典(包名)存在
if (!bodyTexture.ContainsKey(packageName))
{
bodyTexture[packageName] = new Dictionary<string, Dictionary<string, List<Sprite>>>();
}
// 确保第二层字典(文件路径)存在
if (!bodyTexture[packageName].ContainsKey(filePath))
{
bodyTexture[packageName][filePath] = new Dictionary<string, List<Sprite>>();
}
// 确保第三层字典(身体部件名)存在
if (!bodyTexture[packageName][filePath].ContainsKey(bodyPartName))
{
bodyTexture[packageName][filePath][bodyPartName] = new List<Sprite>();
}
// 将 Sprite 添加到列表中
bodyTexture[packageName][filePath][bodyPartName].Add(sprite);
}
}
}