(client) feat:实现实体动态创建,实体右键菜单
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: #41
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@ -14,9 +15,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>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
@ -44,15 +47,18 @@ namespace Managers
|
||||
|
||||
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
// 需要链接的定义、需要链接的字段、链接信息
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
|
||||
|
||||
string currentPackID = string.Empty;
|
||||
|
||||
void ProcessDefine(Define def, Define parentDef, FieldInfo parentField)
|
||||
|
||||
void ProcessDefine(Define def)
|
||||
{
|
||||
if (def == null || def.isReferene || processedDefines.Contains(def))
|
||||
if (def == null || def.isReferene)
|
||||
return;
|
||||
|
||||
processedDefines.Add(def);
|
||||
def.packID = currentPackID;
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
@ -60,57 +66,123 @@ namespace Managers
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
|
||||
.ToArray();
|
||||
.ToArray(); // 不再过滤,先获取所有字段
|
||||
|
||||
// 缓存字段信息
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
foreach (var defineField in defineFields)
|
||||
{
|
||||
var defRef = (Define)defineField.GetValue(def);
|
||||
if (defRef == null)
|
||||
continue;
|
||||
if (defRef.isReferene)
|
||||
var fieldType = defineField.FieldType;
|
||||
|
||||
// 处理单个 Define 类型的字段
|
||||
if (typeof(Define).IsAssignableFrom(fieldType))
|
||||
{
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, defRef));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(defRef.defName))
|
||||
var defRef = (Define)defineField.GetValue(def);
|
||||
if (defRef == null)
|
||||
continue;
|
||||
|
||||
if (defRef.isReferene)
|
||||
{
|
||||
var typeName = defRef.GetType().Name;
|
||||
if (!anonymousDefines.ContainsKey(typeName))
|
||||
anonymousDefines.Add(typeName, new List<Define>());
|
||||
anonymousDefines[typeName].Add(defRef);
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(def, defineField, defRef));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(defRef.defName))
|
||||
{
|
||||
var typeName = defRef.GetType().Name;
|
||||
if (!anonymousDefines.ContainsKey(typeName))
|
||||
anonymousDefines.Add(typeName, new List<Define>());
|
||||
anonymousDefines[typeName].Add(defRef);
|
||||
}
|
||||
ProcessDefine(defRef);
|
||||
}
|
||||
}
|
||||
// 处理集合类型字段
|
||||
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
var elementType = fieldType.GenericTypeArguments[0];
|
||||
if (typeof(Define).IsAssignableFrom(elementType))
|
||||
{
|
||||
var list = (IList)defineField.GetValue(def);
|
||||
if (list != null)
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
if (item is Define defItem && !defItem.isReferene)
|
||||
{
|
||||
ProcessDefine(defItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 处理数组类型字段
|
||||
else if (fieldType.IsArray)
|
||||
{
|
||||
var elementType = fieldType.GetElementType();
|
||||
if (typeof(Define).IsAssignableFrom(elementType))
|
||||
{
|
||||
var array = (Array)defineField.GetValue(def);
|
||||
if (array != null)
|
||||
{
|
||||
foreach (var item in array)
|
||||
{
|
||||
if (item is Define defItem && !defItem.isReferene)
|
||||
{
|
||||
ProcessDefine(defItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ProcessDefine(defRef, def, defineField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
foreach (var define in pack.Value.defines)
|
||||
currentPackID = pack.Value.packID;
|
||||
foreach (var (typeName, defList) in pack.Value.defines)
|
||||
{
|
||||
var typeName = define.Key;
|
||||
var defList = define.Value;
|
||||
|
||||
if (!defines.ContainsKey(typeName))
|
||||
defines[typeName] = new Dictionary<string, Define>();
|
||||
|
||||
|
||||
foreach (var def in defList)
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
||||
// 处理顶层 Define
|
||||
ProcessDefine(def, null, null);
|
||||
ProcessDefine(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var defRef in defineCache)
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
|
||||
if (defRef.Item1 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item1 为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defRef.Item2 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item2 为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
var value = FindDefine(defRef.Item3.description, defRef.Item3.defName);
|
||||
if (value == null)
|
||||
{
|
||||
Debug.LogError($"FindDefine 返回 null: description={defRef.Item3.description}, defName={defRef.Item3.defName}");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"SetValue 出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +210,23 @@ namespace Managers
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用模板查找并返回指定类型的 Define 对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型</typeparam>
|
||||
/// <param name="defineName">定义名</param>
|
||||
/// <returns>如果找到,返回转换为目标类型的 Define 对象;否则返回 null。</returns>
|
||||
public T FindDefine<T>(string defineName) where T : Define
|
||||
{
|
||||
foreach (var typeDict in defines.Values)
|
||||
{
|
||||
if (typeDict.TryGetValue(defineName, out var define) && define is T result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public DefinePack GetDefinePackage(Define define)
|
||||
{
|
||||
if (define == null || define.packID == null)
|
||||
@ -340,4 +428,5 @@ namespace Managers
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
133
Client/Assets/Scripts/Managers/EntityManage.cs
Normal file
133
Client/Assets/Scripts/Managers/EntityManage.cs
Normal file
@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class EntityManage:Utils.MonoSingleton<EntityManage>,ITick
|
||||
{
|
||||
public Dictionary<string, List<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var faction in factionEntities)
|
||||
{
|
||||
List<EntityPrefab> entitiesToRemove = new List<EntityPrefab>();
|
||||
|
||||
foreach (var entityPrefab in faction.Value)
|
||||
{
|
||||
if (entityPrefab.entity.IsDead)
|
||||
{
|
||||
entitiesToRemove.Add(entityPrefab);
|
||||
}
|
||||
else
|
||||
{
|
||||
ITick itike = entityPrefab.entity;
|
||||
itike.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有标记为死亡的实体
|
||||
foreach (var entityToRemove in entitiesToRemove)
|
||||
{
|
||||
faction.Value.Remove(entityToRemove);
|
||||
Destroy(entityToRemove.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据给定的PawnDef生成一个实体对象。
|
||||
/// </summary>
|
||||
/// <param name="pawnDef">定义实体属性的PawnDef对象。</param>
|
||||
/// <param name="pos">实体生成的位置。</param>
|
||||
/// <remarks>
|
||||
/// 1. 如果entityPrefab或pawnDef为null,则不会生成实体。
|
||||
/// 2. 实体将被创建在entityLevel.transform下。
|
||||
/// 3. 使用EntityPrefab组件初始化实体。
|
||||
/// </remarks>
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
// 检查entityPrefab是否为空
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查pawnDef是否为空
|
||||
if (pawnDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 实例化实体对象
|
||||
var entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
|
||||
// 获取EntityPrefab组件
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查EntityPrefab组件是否存在
|
||||
if (entityComponent == null)
|
||||
{
|
||||
Debug.LogError($"Error: EntityPrefab component not found on the instantiated object: {entity.name}");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化实体组件
|
||||
entityComponent.Init(pawnDef);
|
||||
// 确保派系键存在,并初始化对应的列表
|
||||
var factionKey = pawnDef.attributes.label == null ? "default" : pawnDef.attributes.label;
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
|
||||
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>();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/EntityManage.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/EntityManage.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa378b7511b04429b8b6b0efbcce825a
|
||||
timeCreated: 1753149728
|
@ -1,22 +0,0 @@
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class Generator:MonoBehaviour
|
||||
{
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
if (entityPrefab == null || pawnDef == null)
|
||||
return;
|
||||
|
||||
GameObject entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// entity.name = pawnDef.name;
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
entityComponent?.Init(pawnDef);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d78f1b5a44344a4a987e308d3b9478cc
|
||||
timeCreated: 1752937967
|
@ -1,68 +1,151 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
|
||||
{
|
||||
public Dictionary<string, Texture2D> packagesImages = new();
|
||||
public Dictionary<string, Sprite> sprites = new();
|
||||
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, 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)
|
||||
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;
|
||||
packagesImages.Add(ima.name, texture);
|
||||
SplitTextureIntoSprites(ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
|
||||
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);
|
||||
InsertBodyTexture(drawOrder.packID, drawOrder.texturePath, name, spr);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SplitTextureIntoSprites(string name, Texture2D texture, int rows, int cols, int pixelsPerUnit)
|
||||
private void SplitTextureIntoSprites(
|
||||
string packId,
|
||||
string baseName,
|
||||
Texture2D texture,
|
||||
int rows,
|
||||
int cols,
|
||||
int pixelsPerUnit)
|
||||
{
|
||||
if (texture == null || rows <= 0 || cols <= 0)
|
||||
if (texture == null)
|
||||
{
|
||||
Debug.LogError("Invalid parameters for splitting texture.");
|
||||
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>();
|
||||
|
||||
var 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++)
|
||||
{
|
||||
// 计算当前小块的矩形区域
|
||||
var spriteRect = new Rect(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
|
||||
|
||||
// 创建Sprite
|
||||
var sprite = Sprite.Create(texture, (Rect)spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
||||
|
||||
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;
|
||||
sprites[name + $"_{index}"] = sprite;
|
||||
var spriteName = $"{baseName}_{index}";
|
||||
|
||||
sprites[packId][spriteName] = sprite;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -70,18 +153,124 @@ namespace Managers
|
||||
public void Reload()
|
||||
{
|
||||
packagesImages.Clear();
|
||||
sprites.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
public Sprite GetSprite(string name)
|
||||
|
||||
public Sprite GetSprite(string packID, string name)
|
||||
{
|
||||
return sprites.GetValueOrDefault(name, null);
|
||||
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 name, int index)
|
||||
public Sprite GetSprite(string packID, string name, int index)
|
||||
{
|
||||
name += $"_{index}";
|
||||
return GetSprite(name);
|
||||
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 Array.Empty<Sprite>();
|
||||
}
|
||||
|
||||
// 检查文件路径是否存在
|
||||
if (!packageDict.TryGetValue(filePath, out var pathDict))
|
||||
{
|
||||
Debug.LogWarning($"File path '{filePath}' not found in package '{packageName}'.");
|
||||
return Array.Empty<Sprite>();
|
||||
}
|
||||
|
||||
// 收集所有匹配的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: value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按编号排序(无编号视为0)
|
||||
return sprites
|
||||
.OrderBy(x => x.order)
|
||||
.Select(x => x.sprite)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user