using Cysharp.Threading.Tasks; using HxGame; using HxGame.Data; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Security.Policy; using System.Threading; using System.Xml; using System.Xml.Linq; using UnityEditor; using UnityEngine; using UnityEngine.Networking; using UnityEngine.Rendering; using UnityEngine.UI; using UnityEngine.UIElements; using static MapManager; public partial class MapManager : MonoBehaviour { [SerializeField] private Transform mapRegion; public Dictionary allMaps = new Dictionary(); public bool LoadObsXml() { if (_curOpenMapId < 0) { UIWindow.Instance.ShowMessage("请先打开地图"); return false; } string path = string.Empty; path = PathUtil.GetXmlPath(_curOpenMapId, "Obs"); if (!File.Exists(path)) { UIWindow.Instance.ShowMessage("没有找到网格文件, 将重新计算格子"); return false; } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNode xmlRoot = xmlDocument.SelectSingleNode("Item"); string ID = xmlRoot.Attributes.GetNamedItem("ID").Value; mapWidth = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("MapWidth").Value); mapHeight = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("MapHeight").Value); int cellCount = 0; _cellWidth = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("CellWidth").Value); _cellHeight = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("CellHeight").Value); _cellRows = mapHeight / _cellHeight; _cellCols = mapWidth / _cellWidth; cellCount = _cellRows * _cellCols; string strData = xmlRoot.Attributes.GetNamedItem("Value").Value; //0,0,1,0;0,1,1,1;0,2,1,2; string[] values = strData.Split(';'); if (values.Length != cellCount) { UIWindow.Instance.ShowMessage("网格数据错误"); return false; } cellsNode = new CellNode[cellCount]; for (int i = 0; i < cellCount; i++) { string[] strCell = values[i].Split(','); if (strCell.Length != 3) continue; int y = Convert.ToInt32(strCell[0]); int x = Convert.ToInt32(strCell[1]); int type = Convert.ToInt32(strCell[2]); //有些格子之前有点问题 CellType celltype = (CellType)type; if ((celltype & CellType.HadRole) > 0) celltype ^= CellType.HadRole; cellsNode[i] = new CellNode(x, y, i, celltype); } return true; } public void LoadRegionXML(int mapId) { Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions"); if (regionsTrans == null) { UIWindow.Instance.ShowMessage("没有找到Regions节点"); return; } MapRegions mapRegions = new MapRegions(); mapRegions.mapWidth = Convert.ToInt32(txtMapWidth.text); mapRegions.mapHeight = Convert.ToInt32(txtMapHeight.text); mapRegions.regionWidth = Convert.ToInt32(txtRegionWidth.text); mapRegions.regionHeight = Convert.ToInt32(txtRegionHeight.text); int row = mapRegions.mapWidth / MapManager.CELLSCALE / mapRegions.regionWidth; int col = mapRegions.mapHeight / MapManager.CELLSCALE / mapRegions.regionHeight; Transform trans = null; mapRegions.regions = new Region[row, col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mapRegions.regions[i, j] = new Region(); mapRegions.regions[i, j].indexX = i; mapRegions.regions[i, j].indexY = j; string regionName = $"{i}_{j}"; trans = regionsTrans.Find(regionName); if (trans == null) { UIWindow.Instance.ShowMessage($"没有找到region节点 name={regionName}"); return; } //mapRegions.regions[i, j].regionName = trans.GetComponent().sprite.name; mapRegions.regions[i, j].regionName = trans.GetComponent().texture.name; } } mapRegions.SaveXML(mapId); } public void LoadMapRegions(int mapId) { _curOpenMapId = mapId; if (!allMaps.TryGetValue(mapId.ToString(),out var mapInfo)) { UIWindow.Instance.ShowMessage("地图不存在。。。"); return; } int mapRownum = mapInfo.maxRow; int mapColumn = mapInfo.maxCol; //平铺; float jpgscenew = 512f / 100; MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum); MapManager.Instance.GenerateMap(jpgscenew * mapColumn, jpgscenew * mapRownum, 0.48f, 0.32f); for (int row = 0; row < mapRownum; row++) { for (int col = 0; col < mapColumn; col++) { string filename = $"v{mapId}_r{row + 1}_c{col + 1}"; // 构造文件名,v1000_r1_c1.jpg string spPath = PathUtil.GetMapTexure(1000, filename, "jpg"); GameObject obj = new GameObject(filename); obj.transform.SetParent(mapRegion); SpriteRenderer sr = obj.AddComponent(); float x = col * jpgscenew; float y = (mapRownum - row - 1) * jpgscenew; obj.transform.position = new Vector2(x, y); multithreadLoadTextrue(spPath,sr); } } } public void ClearMapRegions() { ClearMapGrid(); foreach (Transform child in mapRegion) { Destroy(child.gameObject); } } private async void multithreadLoadTextrue(string fullPath,SpriteRenderer sr) { var texture = await loadASyncTexture2D(fullPath, this.GetCancellationTokenOnDestroy()); if (texture != null) { sr.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, 100f); } } private UniTask loadASyncTexture2D(string fullPath,CancellationToken token) { return UniTask.RunOnThreadPool(async () => { if (!File.Exists(fullPath)) { Debug.LogError($"File not found: {fullPath}"); return null; } // 在线程池中异步读取文件 byte[] bytes = await File.ReadAllBytesAsync(fullPath, token); // 切换到主线程处理纹理 await UniTask.Yield(PlayerLoopTiming.Update); Texture2D texture = new Texture2D(2, 2); // 初始大小 if (texture.LoadImage(bytes)) { return texture; } else { Debug.LogError($"Failed to load image from {fullPath}"); return null; } }); } IEnumerator LoadSpriteT(string spName, GameObject go, SpriteRenderer sprite) { if (File.Exists(spName)) { byte[] bytes = File.ReadAllBytes(spName); Texture2D texture = new Texture2D(2, 2); // 初始大小,稍后调整 if (texture.LoadImage(bytes)) // 解码 JPG 到 Texture2D { sprite.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, 100f); Debug.Log($"Loaded {spName} successfully"); } else { Debug.LogError($"Failed to load image from {spName}"); } } else { Debug.LogError($"File not found: {spName}"); } yield break; //using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(spName)) //{ // yield return req.SendWebRequest(); // if (req.result == UnityWebRequest.Result.Success) // { // Texture2D tt = DownloadHandlerTexture.GetContent(req); // if (tt == null) // yield break; // sprite.sprite = Sprite.Create(tt, new Rect(0, 0, tt.width, tt.height), Vector2.zero, 100f); // } // else // { // UIWindow.Instance.ShowMessage(req.error); // } //} } internal void SaveCellsXml() { throw new NotImplementedException(); } }