(client) feat:添加自定义瓦片和图片资源自定义加载 (#38)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-07-17 15:42:24 +08:00
committed by TheRedApricot
parent ffeb65ba6b
commit 44cfb55985
42 changed files with 461 additions and 70 deletions

View File

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using Formatting = Newtonsoft.Json.Formatting;
namespace Configs
@ -284,5 +287,57 @@ namespace Configs
return resourceDict;
}
/// <summary>
/// 从外部指定文件中加载图片
/// </summary>
/// <param name="filePath">图片文件的完整路径</param>
/// <returns>加载成功的 Texture2D 对象,或加载失败时返回 null</returns>
public static Texture2D LoadTextureByIO(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
Debug.LogError("文件路径为空,请检查输入!");
return null;
}
// 检查文件是否存在
if (!System.IO.File.Exists(filePath))
{
Debug.LogError($"文件不存在: {filePath}");
return null;
}
byte[] bytes = null;
try
{
// 使用 using 自动管理 FileStream 的生命周期
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fs.Seek(0, SeekOrigin.Begin); // 将游标移动到文件开头(可选)
bytes = new byte[fs.Length]; // 创建一个字节数组来存储文件内容
fs.Read(bytes, 0, bytes.Length); // 读取文件内容到字节数组
}
}
catch (Exception e)
{
Debug.LogError($"读取文件时发生错误: {e.Message}");
return null;
}
// 创建一个默认大小的 Texture2D 对象
Texture2D texture = new Texture2D(2, 2); // 初始大小为 2x2LoadImage 会自动调整大小
if (texture.LoadImage(bytes)) // 加载图片数据
{
return texture; // 返回加载成功的 Texture2D 对象
}
else
{
Debug.LogError("图片加载失败,请检查文件格式是否正确!");
return null;
}
}
}
}

View File

@ -8,13 +8,15 @@ using System.Xml.Linq;
namespace Data
{
public abstract class Define
public class Define
{
public string defName;
public string description;
public string label;
public string packID;
public bool isReferene=false;
/// <summary>
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef" />) 进行处理。
/// </summary>
@ -112,18 +114,5 @@ namespace Data
return string.Join(Environment.NewLine, text.Split('\n').Select(line => prefix + line));
}
}
public class DefineReference : Define
{
public Define def;
public string className;
public string fieldName;
public DefineReference(string className, string defName, string fieldName)
{
this.defName = defName;
this.className = className;
this.fieldName = fieldName;
}
}
}

View File

@ -107,10 +107,19 @@ namespace Data
public PackAbout packAbout;
public string packID;
public string packRootPath;
public string Name
{
get
{
return packAbout.name;
}
}
public bool LoadPack(string packPath)
{
packRootPath=System.IO.Path.GetFullPath(packPath);;
var packDatas = ConfigProcessor.LoadXmlFromPath(packPath);
var aboutXmls = FindDocumentsWithRootName(packDatas, "About");
if (aboutXmls == null || aboutXmls.Count < 1)
@ -262,8 +271,12 @@ namespace Data
}
else
{
value = new DefineReference(field.FieldType.Name, element.Value, field.Name);
var reference = (Define)Activator.CreateInstance(field.FieldType);
reference.isReferene = true;
reference.description=field.FieldType.Name;
reference.label = field.Name;
reference.defName = element.Value;
value = reference;
}
}
else

View File

@ -8,7 +8,7 @@ namespace Data
{
public class TileDef : Define
{
public string texturePath = "";
public ImageDef texture;
public string name = "";
public override bool Init(XElement xmlDef)
@ -45,6 +45,7 @@ namespace Data
return true;
}
}
}

View File

@ -0,0 +1,20 @@
using System.Xml.Linq;
namespace Data
{
public class ImageDef : Define
{
public string name;
public string path;
public int wCount;
public int hCount;
public int pixelsPerUnit = 16;
public override bool Init(XElement xmlDef)
{
base.Init(xmlDef);
name = defName;
return false;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8988509c3c0f4525871f5ccd7ef28363
timeCreated: 1752672894

View File

@ -2,7 +2,11 @@ namespace Entity
{
public class Monster
{
public Protocol.MonsterPack ToPack()
{
var pack= new Protocol.MonsterPack();
return pack;
}
}
public class MonsterAttributes

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Data;
using UnityEngine;
using Utils;
@ -30,52 +32,53 @@ namespace Managers
var pack = new DefinePack();
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
}
List<Tuple<Define,DefineReference>> defineRefs = new();
Dictionary<Type, FieldInfo[]> fieldCache = new();
//不优化到循环里面是因为要先建立索引再链接
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
foreach (var pack in packs)
{
foreach (var define in pack.Value.defines)
{
var typeName=define.Key;
var defList=define.Value;
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;
if (def is DefineReference reference)
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{
defineRefs.Add(new(def,reference));
// 获取所有字段类型为 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));
}
}
}
}
foreach (var defineRef in defineRefs)
foreach (var defRef in defineCache)
{
var define = defineRef.Item1;
var reference = defineRef.Item2;
var referenceDef=FindDefine(reference.className,define.defName);
var property = define.GetType().GetProperty(reference.fieldName);
if (property != null && property.CanWrite)
{
property.SetValue(define, referenceDef);
}
else
{
// 如果是字段而不是属性
var field = define.GetType().GetField(reference.fieldName);
if (field != null)
{
field.SetValue(define, referenceDef);
}
else
{
// 处理找不到成员的情况
Debug.LogError($"Could not find field or property '{reference.fieldName}' in type {define.GetType().Name}");
}
}
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
}
}
/// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。
@ -94,6 +97,133 @@ namespace Managers
}
return null;
}
public DefinePack GetDefinePackage(Define define)
{
if (define == null || define.packID == null)
return null;
packs.TryGetValue(define.packID, out var pack);
return pack;
}
public string GetDefinePackageName(Define define)
{
return GetDefinePackage(define)?.Name;
}
public string GetPackagePath(string packID)
{
if (packs.TryGetValue(packID, out var pack))
{
return pack.packRootPath;
}
return null;
}
public Define[] GetAllDefine()
{
List<Define> defineList = new();
foreach (var define in defines.Values)
{
defineList.AddRange(define.Values);
}
return defineList.ToArray();
}
/// <summary>
/// 查询 Define 对象。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <param name="defineName">定义名(内层字典的键)。</param>
/// <returns>如果找到,则返回 Define 对象;否则返回 null。</returns>
public Define QueryDefine(string defineType, string defineName)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
if (string.IsNullOrEmpty(defineName))
{
Debug.LogError("查询失败:定义名参数不能为空!");
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
if (!typeDefinitions.TryGetValue(defineName, out var targetDefine))
{
Debug.LogWarning($"查询失败:定义类型 '{defineType}' 中未找到定义名 '{defineName}'");
return null;
}
return targetDefine;
}
/// <summary>
/// 查询指定类型下的所有 Define 对象。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
public Define[] QueryDefinesByType(string defineType)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
return typeDefinitions.Values.ToArray();
}
/// <summary>
/// 查询指定类型下的所有 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))
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
try
{
// 获取所有值并尝试转换为目标类型
var result = new List<T>();
foreach (var item in typeDefinitions.Values)
{
if (item is T converted)
{
result.Add(converted);
}
else
{
Debug.LogError($"类型转换失败:无法将 {item.GetType().Name} 转换为 {typeof(T).Name}");
return null;
}
}
return result.ToArray();
}
catch (Exception ex)
{
Debug.LogError($"类型转换失败:从 Define 转换为 {typeof(T).Name} 时出错。错误信息:{ex.Message}");
return null;
}
}
public override string ToString()
{
if (packs == null || packs.Count == 0)

View File

@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.IO;
using Data;
using UnityEngine;
namespace Managers
{
public class PackagesImageManager : Utils.Singleton<PackagesImageManager>
{
public Dictionary<string, Texture2D> packagesImages = new();
public Dictionary<string, Sprite> sprites = new();
public void Init()
{
if (packagesImages.Count > 0)
return;
var imageDef = Managers.DefineManager.Instance.QueryDefinesByType<ImageDef>();
foreach (var ima in imageDef)
{
if (ima.path == 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);
}
}
void SplitTextureIntoSprites(string name, Texture2D texture, int rows, int cols, int pixelsPerUnit)
{
if (texture == null || rows <= 0 || cols <= 0)
{
Debug.LogError("Invalid parameters for splitting texture.");
return;
}
var textureWidth = texture.width;
var textureHeight = texture.height;
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;
}
// 遍历每一行和每一列
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);
var index = row * cols + col;
sprites[name + $"_{index}"] = sprite;
}
}
}
public void Reload()
{
packagesImages.Clear();
Init();
}
public Sprite GetSprite(string name)
{
return sprites.GetValueOrDefault(name, null);
}
public Sprite GetSprite(string name, int index)
{
name += $"_{index}";
return GetSprite(name);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a3bbc0fa9efd4f018e27a6b9f2845140
timeCreated: 1752672790

View File

@ -1,7 +0,0 @@
namespace Managers
{
public class TileMapManager
{
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: b9c65bbe278844978d49c3cd26c050ed
timeCreated: 1752248825

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Data;
using UnityEngine;
using UnityEngine.Tilemaps;
@ -7,26 +8,105 @@ namespace Map
{
public class DoubleMap : MonoBehaviour
{
public Tilemap dataLevel;
public List<List<int>> mapData = new();
// public Tilemap dataLevel;
public Tilemap textureLevel;
public Dictionary<string, TileBase> tileDict = new();
void Start()
{
tileDict = Configs.ConfigProcessor.LoadResources<TileBase>("TileMap");
var tile= tileDict.Values;
for (int i = 0; i < 100; i++)
TileManager.Instance.Init();
}
public void UpdateTexture()
{
}
public void SetTile(int x, int y, string tileName)
{
}
public void SetTile(int x, int y, int id)
{
}
}
public class TileManager:Utils.Singleton<TileManager>
{
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
public Dictionary<string, int> tileID = new();
public void Init()
{
if (tileToTileBaseMapping.Count > 0)
return;
Managers.PackagesImageManager.Instance.Init();
var imagePack = Managers.PackagesImageManager.Instance;
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
for (var i = 1; i < tileType.Length; i++)
{
for (int j = 0; j < 100; j++)
tileID.Add(tileType[i].name, i);
}
var tileTextureMappingDef=Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
foreach (var mappingTableDef in tileTextureMappingDef)
{
foreach (var keyVal in mappingTableDef.tileDict)
{
textureLevel.SetTile(new(i,j),tile.First());
var key = keyVal.Key;
var val=keyVal.Value;
var parts = key.Split('_');
if (parts.Length != 4)
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}内容不合法!\n应该为[瓦片名称_瓦片名称_瓦片名称_瓦片名称]的格式");
continue;
}
if (!(tileID.TryGetValue(parts[0], out var k1) &&
tileID.TryGetValue(parts[1], out var k2) &&
tileID.TryGetValue(parts[2], out var k3) &&
tileID.TryGetValue(parts[3], out var k4)))
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}中存在未定义的瓦片名称");
continue;
}
var sprite = imagePack.GetSprite(val);
if (sprite == null)
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{val}中存在未定义的图片名称");
continue;
}
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogWarning($"来自{packName}定义的TileMappingTableDef键值{(k1, k2, k3, k4)}存在重复索引,将忽略重复项");
continue;
}
var tile = LoadTile(sprite);
tileToTileBaseMapping.Add((k1, k2, k3, k4), tile);
}
}
}
}
public class TileMappingTable:Utils.Singleton<TileMappingTable>
{
public void Reload()
{
tileToTileBaseMapping.Clear();
Init();
}
public TileBase LoadTile(Sprite sprite)
{
var newTile = ScriptableObject.CreateInstance<Tile>();
newTile.sprite = sprite;
newTile.color = Color.white;
newTile.colliderType = Tile.ColliderType.Sprite;
return newTile;
}
}
}

View File

@ -1,5 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Define>
<ImageDef>
<defName>GrassDirt</defName>
<path>Resources\Map\GrassSoild.png</path>
<wCount>4</wCount>
<hCount>4</hCount>
</ImageDef>
<TileDef>
<defName>Grass</defName>
<name>Grass</name>
</TileDef>
<TileDef>
<defName>Dirt</defName>
<name>Dirt</name>
</TileDef>
<TileMappingTableDef>
<defName>GrassDirtTable</defName>
<Grass_Grass_Grass_Grass value="GrassDirt_6"/>

View File

@ -3,7 +3,7 @@
<CharacterAttributesDef>
<defName>CatGirl</defName>
<health>100</health>
<moveSpeed>1.2</moveSpeed>
<moveSpeed>2</moveSpeed>
</CharacterAttributesDef>
<CharacterDef>

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

View File

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 656 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 876 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1001 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B