(client) feat:实现身体贴图加载
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Data;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
@ -8,17 +10,25 @@ 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");
|
||||
if(defaultSprite)
|
||||
Debug.Log("加载成功");
|
||||
InitImageDef();
|
||||
InitDrawOrder();
|
||||
packagesImages = null;
|
||||
}
|
||||
|
||||
public void InitImageDef()
|
||||
{
|
||||
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
|
||||
foreach (var ima in imageDef)
|
||||
{
|
||||
@ -38,6 +48,44 @@ namespace Managers
|
||||
|
||||
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(
|
||||
@ -131,5 +179,35 @@ namespace Managers
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user