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; using static UnityEngine.Rendering.DebugUI.Table; public partial class MapManager : MonoBehaviour { [SerializeField] private Transform mapRegionParent; public Transform mapGridParent; public Transform mapAreaParent; public Transform mapMonsterArea; public Transform mapNPCArea; public Transform mapTeleportArea; public Transform mapReliveArea; public Dictionary allMaps = new Dictionary(); /// /// 当前地图区域 /// public MapRegions _curMapRegions; /// /// 保存地图配置 /// /// public void SaveRegionXML() { int mapId = _curOpenMapId; if (!allMaps.TryGetValue(_curOpenMapId.ToString(), out var curMap)) { UIWindow.Instance.ShowMessage($"没有扫描到当前地图 [{_curOpenMapId}]"); return; } _curMapRegions?.SaveXML(mapId); } public void LoadMapRegionSprites(int mapId) { if (!allMaps.TryGetValue(mapId.ToString(),out var mapInfo)) { UIWindow.Instance.ShowMessage("地图不存在。。。"); return; } int mapRownum = mapInfo.maxRow; int mapColumn = mapInfo.maxCol; _curMapRegions = new MapRegions(); _curOpenMapId = mapId; _curMapRegions.regionRowNum = mapRownum; _curMapRegions.regionColNum = mapColumn; _curMapRegions.regionWidth = PicMapPixel; _curMapRegions.regionHeight = PicMapPixel; _curMapRegions.cellWidthPixel = widthPixel; _curMapRegions.cellHeightPixel = heightPixel; _curMapRegions.regions = new Region[mapRownum, mapColumn]; float jpgscenew = PicMapPixel / 100.0f; 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(mapRegionParent); SpriteRenderer sr = obj.AddComponent(); float x = col * jpgscenew; float y = (mapRownum - row - 1) * jpgscenew; obj.transform.position = new Vector2(x, y); _curMapRegions.regions[row, col] = new Region(); _curMapRegions.regions[row, col].indexX = row; _curMapRegions.regions[row, col].indexY = col; multithreadLoadTextrue(spPath, sr); int ldx = mapRownum - row; int ldy = col + 1; _curMapRegions.regions[row, col].regionName = $"v{mapId}_r{ldx}_c{ldy}"; // 构造文件名,v1000_r1_c1.jpg } } MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum); } public void ClearMapRegions() { ClearMapGrid(); foreach (Transform child in mapRegionParent) { Destroy(child.gameObject); } } public void CreateObs() { if(_curMapRegions == null) return; float jpgscenew = PicMapPixel / 100.0f; MapManager.Instance.GenerateMap(jpgscenew * _curMapRegions.regionColNum, jpgscenew * _curMapRegions.regionRowNum, _curMapRegions.cellWidthPixel / 100.0f, _curMapRegions.cellHeightPixel / 100.0f); } 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); // } //} } }