(client) fix:修复匿名定义中的图片无法加载的问题
This commit is contained in:
@ -2,32 +2,10 @@
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class CharacterAttributesDef : Define
|
||||
public class AttributesDef : Define
|
||||
{
|
||||
public int health = 10;
|
||||
public int moveSpeed = 1;
|
||||
}
|
||||
public class WeaponAttributesDef : Define
|
||||
{
|
||||
public int attack = 1;
|
||||
public int defense = 0;
|
||||
public int attackSpeed = 2;
|
||||
public int attackRange = 3;
|
||||
public int attackTargetCount = 1;
|
||||
}
|
||||
public class MonsterAttributesDef : Define
|
||||
{
|
||||
public int health = 10;
|
||||
public int moveSpeed = 1;
|
||||
public int attack = 1;
|
||||
public int defense = 0;
|
||||
public int attackSpeed = 2;
|
||||
public int attackRange = 3;
|
||||
public int attackTargetCount = 1;
|
||||
}
|
||||
public class BuildingAttributesDef : Define
|
||||
{
|
||||
public int health = 10;
|
||||
public int attack = 1;
|
||||
public int defense = 0;
|
||||
public int attackSpeed = 2;
|
||||
|
@ -22,54 +22,13 @@ namespace Data
|
||||
Animation
|
||||
}
|
||||
|
||||
public class CharacterDef : Define
|
||||
public class CharacterDef : PawnDef
|
||||
{
|
||||
public CharacterAttributesDef attributes;
|
||||
public string texturePath = null;
|
||||
public DrawingOrderDef
|
||||
drawingOrder_down,
|
||||
drawingOrder_up,
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
|
||||
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawingOrderDef result = null;
|
||||
|
||||
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down:
|
||||
result = drawingOrder_down;
|
||||
break;
|
||||
case Orientation.Up:
|
||||
result = drawingOrder_up;
|
||||
break;
|
||||
case Orientation.Left:
|
||||
result = drawingOrder_left;
|
||||
break;
|
||||
case Orientation.Right:
|
||||
result = drawingOrder_right;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid orientation value.");
|
||||
}
|
||||
|
||||
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
|
||||
if (result == null) result = drawingOrder_down;
|
||||
|
||||
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
|
||||
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawingOrderDef : Define
|
||||
{
|
||||
public List<DrawNodeDef> drawNodes = new();
|
||||
|
||||
public override bool Init(XElement xmlDef)
|
||||
{
|
||||
base.Init(xmlDef);
|
||||
|
8
Client/Assets/Scripts/Data/ItemDefine.cs
Normal file
8
Client/Assets/Scripts/Data/ItemDefine.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Data
|
||||
{
|
||||
public class ItemDefine:Define
|
||||
{
|
||||
public ImageDef texture;
|
||||
public AttributesDef attributes;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Data/ItemDefine.cs.meta
Normal file
3
Client/Assets/Scripts/Data/ItemDefine.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4d338c5c7b24cd5a496acb98ebf5e3c
|
||||
timeCreated: 1752760211
|
51
Client/Assets/Scripts/Data/PawnDefine.cs
Normal file
51
Client/Assets/Scripts/Data/PawnDefine.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class PawnDef : Define
|
||||
{
|
||||
public string aiController;
|
||||
public string texturePath = null;
|
||||
public DrawingOrderDef
|
||||
drawingOrder_down,
|
||||
drawingOrder_up,
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawingOrderDef result = null;
|
||||
|
||||
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down:
|
||||
result = drawingOrder_down;
|
||||
break;
|
||||
case Orientation.Up:
|
||||
result = drawingOrder_up;
|
||||
break;
|
||||
case Orientation.Left:
|
||||
result = drawingOrder_left;
|
||||
break;
|
||||
case Orientation.Right:
|
||||
result = drawingOrder_right;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid orientation value.");
|
||||
}
|
||||
|
||||
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
|
||||
if (result == null) result = drawingOrder_down;
|
||||
|
||||
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
|
||||
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public class MonsterDef:PawnDef
|
||||
{
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Data/PawnDefine.cs.meta
Normal file
3
Client/Assets/Scripts/Data/PawnDefine.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a89bc126c0a466799f6ccb963267419
|
||||
timeCreated: 1752751832
|
@ -11,10 +11,11 @@ namespace Managers
|
||||
{
|
||||
public class DefineManager : Singleton<DefineManager>
|
||||
{
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mod" };
|
||||
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
|
||||
|
||||
public Dictionary<string, Dictionary<string, Define>> defines = new();
|
||||
public Dictionary<string, DefinePack> packs = new();
|
||||
public Dictionary<string, List<Define>> anonymousDefines = new();
|
||||
/// <summary>
|
||||
/// 初始化定义管理器,加载所有定义包并构建定义字典。
|
||||
/// </summary>
|
||||
@ -32,11 +33,52 @@ namespace Managers
|
||||
var pack = new DefinePack();
|
||||
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
|
||||
}
|
||||
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
//不优化到循环里面是因为要先建立索引再链接
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
|
||||
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
|
||||
|
||||
void ProcessDefine(Define def, Define parentDef, FieldInfo parentField)
|
||||
{
|
||||
if (def == null || def.isReferene || processedDefines.Contains(def))
|
||||
return;
|
||||
|
||||
processedDefines.Add(def);
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
|
||||
.ToArray();
|
||||
// 缓存字段信息
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
foreach (var defineField in defineFields)
|
||||
{
|
||||
var defRef = (Define)defineField.GetValue(def);
|
||||
if (defRef == null)
|
||||
continue;
|
||||
if (defRef.isReferene)
|
||||
{
|
||||
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, 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, def, defineField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pack in packs)
|
||||
{
|
||||
foreach (var define in pack.Value.defines)
|
||||
@ -51,34 +93,16 @@ namespace Managers
|
||||
{
|
||||
defines[typeName][def.defName] = def;
|
||||
|
||||
// 如果字段信息已经缓存,则直接使用缓存
|
||||
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
|
||||
{
|
||||
// 获取所有字段类型为 Define 或其派生类型的字段
|
||||
defineFields = def.GetType()
|
||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
|
||||
.ToArray();
|
||||
// 缓存字段信息
|
||||
fieldCache[def.GetType()] = defineFields;
|
||||
}
|
||||
|
||||
foreach (var defineField in defineFields)
|
||||
{
|
||||
var defRef=(Define)defineField.GetValue(def);
|
||||
if (defRef==null || !defRef.isReferene)
|
||||
continue;
|
||||
defineCache.Add(new(def, defineField, defRef));
|
||||
}
|
||||
// 处理顶层 Define
|
||||
ProcessDefine(def, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var defRef in defineCache)
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
|
||||
}
|
||||
|
||||
Debug.Log(anonymousDefines.Count);
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找指定定义类型的定义名对应的 Define 对象。
|
||||
@ -126,6 +150,11 @@ namespace Managers
|
||||
{
|
||||
defineList.AddRange(define.Values);
|
||||
}
|
||||
|
||||
foreach (var anonymousDefine in anonymousDefines)
|
||||
{
|
||||
defineList.AddRange(anonymousDefine.Value);
|
||||
}
|
||||
return defineList.ToArray();
|
||||
}
|
||||
|
||||
@ -160,7 +189,7 @@ namespace Managers
|
||||
return targetDefine;
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象。
|
||||
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
@ -172,39 +201,49 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
List<Define> result = new List<Define>();
|
||||
|
||||
// 从命名定义中查询
|
||||
if (defines.TryGetValue(defineType, out var namedDefinitions))
|
||||
{
|
||||
result.AddRange(namedDefinitions.Values);
|
||||
}
|
||||
|
||||
// 从匿名定义中查询
|
||||
if (anonymousDefines.TryGetValue(defineType, out var anonymousDefinitionList))
|
||||
{
|
||||
result.AddRange(anonymousDefinitionList);
|
||||
}
|
||||
|
||||
// 如果结果为空,则返回 null
|
||||
if (result.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
return typeDefinitions.Values.ToArray();
|
||||
return result.ToArray();
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。(包括匿名定义)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryDefinesByType<T>()
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
List<Define> allDefines = QueryDefinesByType(defineType)?.ToList();
|
||||
if (allDefines == null || allDefines.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取所有值并尝试转换为目标类型
|
||||
// 尝试将所有 Define 对象转换为目标类型 T
|
||||
var result = new List<T>();
|
||||
foreach (var item in typeDefinitions.Values)
|
||||
foreach (var item in allDefines)
|
||||
{
|
||||
if (item is T converted)
|
||||
{
|
||||
@ -216,6 +255,7 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -16,7 +16,7 @@ namespace Map
|
||||
void Start()
|
||||
{
|
||||
TileManager.Instance.Init();
|
||||
var mapSize = 100;
|
||||
var mapSize = 10;
|
||||
float noiseScale = 0.1f;
|
||||
|
||||
for (int x = 0; x < mapSize; x++)
|
||||
@ -39,11 +39,6 @@ namespace Map
|
||||
mapData.Add(col);
|
||||
UpdateTexture();
|
||||
}
|
||||
|
||||
for (int x = 0; x < 16; x++)
|
||||
{
|
||||
textureLevel.SetTile(new(x,-1),TileManager.Instance.tileBaseMapping.GetValueOrDefault($"GrassDirt_{x}"));
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTexture()
|
||||
|
8
Client/Assets/Scripts/UI.meta
Normal file
8
Client/Assets/Scripts/UI.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6303ff87d5e946649be1abf4e7feb895
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
26
Client/Assets/Scripts/UI/DevMenuUI.cs
Normal file
26
Client/Assets/Scripts/UI/DevMenuUI.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class DevMenuUI : MonoBehaviour
|
||||
{
|
||||
public GameObject menuContent;
|
||||
|
||||
public GameObject textTemplate;
|
||||
public GameObject buttonTemplate;
|
||||
void Start()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
2
Client/Assets/Scripts/UI/DevMenuUI.cs.meta
Normal file
2
Client/Assets/Scripts/UI/DevMenuUI.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c83e72721411938449d92dd48c76480d
|
Reference in New Issue
Block a user