From f4d22af6745ed44aadd3261b093824be794e6706 Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Thu, 17 Jul 2025 10:37:55 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20feat:=E6=B7=BB=E5=8A=A0=E7=93=A6?= =?UTF-8?q?=E7=89=87=E5=8A=A8=E6=80=81=E5=AE=9A=E4=B9=89=E5=92=8C=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=8A=A8=E6=80=81=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Scripts/Configs/ConfigProcessor.cs | 55 ++++++++ Client/Assets/Scripts/Data/DefinePack.cs | 3 +- Client/Assets/Scripts/Data/MapDefine.cs | 7 +- Client/Assets/Scripts/Data/ResourcesDefine.cs | 20 +++ .../Scripts/Data/ResourcesDefine.cs.meta | 3 + .../Assets/Scripts/Managers/DefineManager.cs | 123 ++++++++++++++++++ .../Scripts/Managers/PackagesImageManager.cs | 76 +++++++++++ .../Managers/PackagesImageManager.cs.meta | 3 + .../Assets/Scripts/Managers/TileMapManager.cs | 7 - .../Scripts/Managers/TileMapManager.cs.meta | 3 - Client/Assets/Scripts/Map/DoubleMap.cs | 31 ++++- Client/Data/Core/Define/Map/Map.xml | 13 +- 12 files changed, 320 insertions(+), 24 deletions(-) create mode 100644 Client/Assets/Scripts/Data/ResourcesDefine.cs create mode 100644 Client/Assets/Scripts/Data/ResourcesDefine.cs.meta create mode 100644 Client/Assets/Scripts/Managers/PackagesImageManager.cs create mode 100644 Client/Assets/Scripts/Managers/PackagesImageManager.cs.meta delete mode 100644 Client/Assets/Scripts/Managers/TileMapManager.cs delete mode 100644 Client/Assets/Scripts/Managers/TileMapManager.cs.meta diff --git a/Client/Assets/Scripts/Configs/ConfigProcessor.cs b/Client/Assets/Scripts/Configs/ConfigProcessor.cs index 3cafbd3..9dc772a 100644 --- a/Client/Assets/Scripts/Configs/ConfigProcessor.cs +++ b/Client/Assets/Scripts/Configs/ConfigProcessor.cs @@ -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; } + + /// + /// 从外部指定文件中加载图片 + /// + /// 图片文件的完整路径 + /// 加载成功的 Texture2D 对象,或加载失败时返回 null + 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); // 初始大小为 2x2,LoadImage 会自动调整大小 + + if (texture.LoadImage(bytes)) // 加载图片数据 + { + return texture; // 返回加载成功的 Texture2D 对象 + } + else + { + Debug.LogError("图片加载失败,请检查文件格式是否正确!"); + return null; + } + } } } \ No newline at end of file diff --git a/Client/Assets/Scripts/Data/DefinePack.cs b/Client/Assets/Scripts/Data/DefinePack.cs index 0e8e11e..88037f6 100644 --- a/Client/Assets/Scripts/Data/DefinePack.cs +++ b/Client/Assets/Scripts/Data/DefinePack.cs @@ -107,10 +107,11 @@ namespace Data public PackAbout packAbout; public string packID; - + public string packRootPath; 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) diff --git a/Client/Assets/Scripts/Data/MapDefine.cs b/Client/Assets/Scripts/Data/MapDefine.cs index b49f370..aaac497 100644 --- a/Client/Assets/Scripts/Data/MapDefine.cs +++ b/Client/Assets/Scripts/Data/MapDefine.cs @@ -46,11 +46,6 @@ namespace Data } } - public class ImageDef : Define - { - public string path; - public int wCount; - public int hCount; - } + } \ No newline at end of file diff --git a/Client/Assets/Scripts/Data/ResourcesDefine.cs b/Client/Assets/Scripts/Data/ResourcesDefine.cs new file mode 100644 index 0000000..72ab58c --- /dev/null +++ b/Client/Assets/Scripts/Data/ResourcesDefine.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Data/ResourcesDefine.cs.meta b/Client/Assets/Scripts/Data/ResourcesDefine.cs.meta new file mode 100644 index 0000000..370a88b --- /dev/null +++ b/Client/Assets/Scripts/Data/ResourcesDefine.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8988509c3c0f4525871f5ccd7ef28363 +timeCreated: 1752672894 \ No newline at end of file diff --git a/Client/Assets/Scripts/Managers/DefineManager.cs b/Client/Assets/Scripts/Managers/DefineManager.cs index a9f3bce..7a78f2f 100644 --- a/Client/Assets/Scripts/Managers/DefineManager.cs +++ b/Client/Assets/Scripts/Managers/DefineManager.cs @@ -97,6 +97,129 @@ 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 GetPackagePath(string packID) + { + if (packs.TryGetValue(packID, out var pack)) + { + return pack.packRootPath; + } + return null; + } + + public Define[] GetAllDefine() + { + List defineList = new(); + foreach (var define in defines.Values) + { + defineList.AddRange(define.Values); + } + return defineList.ToArray(); + } + + /// + /// 查询 Define 对象。 + /// + /// 定义类型(外层字典的键)。 + /// 定义名(内层字典的键)。 + /// 如果找到,则返回 Define 对象;否则返回 null。 + 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; + } + /// + /// 查询指定类型下的所有 Define 对象。 + /// + /// 定义类型(外层字典的键)。 + /// 该类型下的 Define 数组,如果未找到则返回 null。 + 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(); + } + /// + /// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。 + /// + /// 目标类型。 + /// 转换后的目标类型数组,如果未找到或转换失败则返回 null。 + public T[] QueryDefinesByType() + { + 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(); + 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) diff --git a/Client/Assets/Scripts/Managers/PackagesImageManager.cs b/Client/Assets/Scripts/Managers/PackagesImageManager.cs new file mode 100644 index 0000000..01493f7 --- /dev/null +++ b/Client/Assets/Scripts/Managers/PackagesImageManager.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using System.IO; +using Data; +using UnityEngine; + +namespace Managers +{ + public class PackagesImageManager : Utils.Singleton + { + public Dictionary packagesImages = new(); + public Dictionary sprites = new(); + + public void Init() + { + if (packagesImages.Count > 0) + return; + var imageDef = Managers.DefineManager.Instance.QueryDefinesByType(); + 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(); + } + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Managers/PackagesImageManager.cs.meta b/Client/Assets/Scripts/Managers/PackagesImageManager.cs.meta new file mode 100644 index 0000000..c310713 --- /dev/null +++ b/Client/Assets/Scripts/Managers/PackagesImageManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a3bbc0fa9efd4f018e27a6b9f2845140 +timeCreated: 1752672790 \ No newline at end of file diff --git a/Client/Assets/Scripts/Managers/TileMapManager.cs b/Client/Assets/Scripts/Managers/TileMapManager.cs deleted file mode 100644 index 5f7ecbb..0000000 --- a/Client/Assets/Scripts/Managers/TileMapManager.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Managers -{ - public class TileMapManager - { - - } -} \ No newline at end of file diff --git a/Client/Assets/Scripts/Managers/TileMapManager.cs.meta b/Client/Assets/Scripts/Managers/TileMapManager.cs.meta deleted file mode 100644 index 5e80be3..0000000 --- a/Client/Assets/Scripts/Managers/TileMapManager.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: b9c65bbe278844978d49c3cd26c050ed -timeCreated: 1752248825 \ No newline at end of file diff --git a/Client/Assets/Scripts/Map/DoubleMap.cs b/Client/Assets/Scripts/Map/DoubleMap.cs index 4816ef1..84fda0f 100644 --- a/Client/Assets/Scripts/Map/DoubleMap.cs +++ b/Client/Assets/Scripts/Map/DoubleMap.cs @@ -21,15 +21,42 @@ namespace Map { } + + public void SetTile(int x, int y, string tileName) + { + + } + + public void SetTile(int x, int y, int id) + { + + } } public class TileManager:Utils.Singleton { - Dictionary tileDict = new(); + public Dictionary<(int, int, int, int), TileBase> TileToTileBaseMapping = new(); public void Init() { - + if( TileToTileBaseMapping.Count>0||!Managers.DefineManager.Instance.defines.TryGetValue("TileDef",out var tileType)) + { + return; + } + Managers.PackagesImageManager.Instance.Init(); + foreach (var tileDef in tileType.Values) + { + var packPath= Managers.DefineManager.Instance.GetDefinePackage(tileDef)?.packRootPath; + if(packPath==null) + continue; + + } + } + + public void Reload() + { + TileToTileBaseMapping.Clear(); + Init(); } } } diff --git a/Client/Data/Core/Define/Map/Map.xml b/Client/Data/Core/Define/Map/Map.xml index b73c6fd..a9778d4 100644 --- a/Client/Data/Core/Define/Map/Map.xml +++ b/Client/Data/Core/Define/Map/Map.xml @@ -1,16 +1,19 @@ - GrassDirtTexture - Map\GrassSoild.png + GrassDirt + Resources\Map\GrassSoild.png 4 4 - GrassDirtTiles - GrassDirtTexture - GrassDirt + Grass + Grass + + + Dirt + Dirt