(client) feat:实现身体贴图加载
This commit is contained in:
@ -14,9 +14,11 @@ namespace Managers
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
{
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
|
||||
|
||||
//类别,定义名,定义
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
//包id,包
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
//类别,定义
|
||||
public Dictionary<string, List<Define>> anonymousDefines = new();
|
||||
/// <summary>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
@ -46,12 +48,14 @@ namespace Managers
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
// 需要链接的定义、需要链接的字段、链接信息
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
|
||||
string currentPackID = string.Empty;
|
||||
|
||||
void ProcessDefine(Define def)
|
||||
{
|
||||
if (def == null || def.isReferene)
|
||||
return;
|
||||
|
||||
def.packID = currentPackID;
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
@ -89,11 +93,12 @@ namespace Managers
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
currentPackID = pack.Value.packID;
|
||||
foreach (var (typeName, defList) in pack.Value.defines)
|
||||
{
|
||||
if (!defines.ContainsKey(typeName))
|
||||
defines[typeName] = new Dictionary<string, Define>();
|
||||
|
||||
|
||||
foreach (var def in defList)
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
@ -12,6 +13,8 @@ namespace Managers
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
@ -57,6 +60,7 @@ namespace Managers
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -64,6 +68,7 @@ namespace Managers
|
||||
if (pawnDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -79,6 +84,7 @@ namespace Managers
|
||||
if (entityComponent == null)
|
||||
{
|
||||
Debug.LogError($"Error: EntityPrefab component not found on the instantiated object: {entity.name}");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -96,12 +102,32 @@ namespace Managers
|
||||
{
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}");
|
||||
GenerateDefaultEntity(pos);
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
const string factionKey = "default";
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
protected override void OnStart()
|
||||
{
|
||||
factionEntities.Clear();
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
||||
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -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