Files
HX_MapEditor/Assets/Scripts/Map/MapManager.Region.cs

267 lines
9.5 KiB
C#
Raw Normal View History

2025-06-15 22:06:57 +08:00
using Cysharp.Threading.Tasks;
2025-06-14 13:46:24 +08:00
using HxGame;
2025-06-16 21:26:36 +08:00
using HxGame.Data;
2025-06-14 13:46:24 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
2025-06-15 20:14:45 +08:00
using System.Runtime.Remoting.Metadata.W3cXsd2001;
2025-06-16 00:15:41 +08:00
using System.Security.Policy;
using System.Threading;
2025-06-14 13:46:24 +08:00
using System.Xml;
2025-06-15 20:14:45 +08:00
using System.Xml.Linq;
2025-06-14 13:46:24 +08:00
using UnityEditor;
using UnityEngine;
2025-06-15 20:14:45 +08:00
using UnityEngine.Networking;
using UnityEngine.Rendering;
using UnityEngine.UI;
2025-06-16 00:15:41 +08:00
using UnityEngine.UIElements;
2025-06-14 13:46:24 +08:00
using static MapManager;
2025-06-16 23:59:28 +08:00
using static UnityEngine.Rendering.DebugUI.Table;
2025-06-14 13:46:24 +08:00
public partial class MapManager : MonoBehaviour
2025-06-16 00:15:41 +08:00
{
[SerializeField]
private Transform mapRegion;
2025-06-16 23:59:28 +08:00
public Dictionary<string, (int maxRow, int maxCol)> allMaps = new Dictionary<string, (int maxRow, int maxCol)>();
/// <summary>
/// <20><>ǰ<EFBFBD><C7B0>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
/// </summary>
2025-06-17 00:23:38 +08:00
public MapRegions _curMapRegions;
2025-06-14 13:46:24 +08:00
public bool LoadObsXml()
{
if (_curOpenMapId < 0)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȴ򿪵<C8B4>ͼ");
return false;
}
2025-06-15 20:14:45 +08:00
string path = string.Empty;
2025-06-14 13:46:24 +08:00
path = PathUtil.GetXmlPath(_curOpenMapId, "Obs");
if (!File.Exists(path))
{
UIWindow.Instance.ShowMessage(<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
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(';');
2025-06-15 20:14:45 +08:00
if (values.Length != cellCount)
2025-06-14 13:46:24 +08:00
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>");
return false;
}
cellsNode = new CellNode[cellCount];
2025-06-15 20:14:45 +08:00
for (int i = 0; i < cellCount; i++)
2025-06-14 13:46:24 +08:00
{
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]);
//<2F><>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD>֮ǰ<D6AE>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
CellType celltype = (CellType)type;
if ((celltype & CellType.HadRole) > 0)
celltype ^= CellType.HadRole;
cellsNode[i] = new CellNode(x, y, i, celltype);
}
return true;
}
2025-06-16 23:59:28 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
/// </summary>
/// <param name="mapId"></param>
public void SaveRegionXML()
2025-06-16 21:26:36 +08:00
{
2025-06-16 23:59:28 +08:00
int mapId = _curOpenMapId;
if (!allMaps.TryGetValue(_curOpenMapId.ToString(), out var curMap))
2025-06-16 21:26:36 +08:00
{
2025-06-16 23:59:28 +08:00
UIWindow.Instance.ShowMessage($"û<><C3BB>ɨ<EFBFBD><EFBFBD><E8B5BD>ǰ<EFBFBD><C7B0>ͼ [{_curOpenMapId}]");
2025-06-16 21:26:36 +08:00
return;
}
2025-06-16 23:59:28 +08:00
_curMapRegions?.SaveXML(mapId);
//MapRegions mapRegions = new MapRegions();
//mapRegions.regionRowNum = curMap.maxRow;
//mapRegions.regionColNum = curMap.maxCol;
//mapRegions.regionWidth = PicMapPixel;
//mapRegions.regionHeight = PicMapPixel;
//mapRegions.cellWidthPixel = widthPixel;
//mapRegions.cellHeightPixel = heightPixel;
//mapRegions.regions = new Region[mapRegions.regionRowNum, mapRegions.regionColNum];
//for (int row = 0; row < mapRegions.regionRowNum; row++)
//{
// for (int col = 0; col < mapRegions.regionColNum; col++)
// {
// mapRegions.regions[row, col] = new Region();
// mapRegions.regions[row, col].indexX = row;
// mapRegions.regions[row, col].indexY = col;
// int ldx = mapRegions.regionRowNum - row;
// int ldy = col + 1;
// mapRegions.regions[row, col].regionName = $"v{mapId}_r{ldx}_c{ldy}"; // <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>v1000_r1_c1.jpg
// }
//}
//mapRegions.SaveXML(mapId);
2025-06-16 21:26:36 +08:00
}
2025-06-16 00:15:41 +08:00
2025-06-16 23:59:28 +08:00
public void LoadMapRegionSprites(int mapId)
2025-06-15 20:14:45 +08:00
{
2025-06-16 00:15:41 +08:00
if (!allMaps.TryGetValue(mapId.ToString(),out var mapInfo))
{
UIWindow.Instance.ShowMessage("<22><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ڡ<EFBFBD><DAA1><EFBFBD><EFBFBD><EFBFBD>");
return;
}
int mapRownum = mapInfo.maxRow;
int mapColumn = mapInfo.maxCol;
2025-06-16 23:59:28 +08:00
_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;
2025-06-15 20:14:45 +08:00
for (int row = 0; row < mapRownum; row++)
{
for (int col = 0; col < mapColumn; col++)
{
2025-06-16 00:15:41 +08:00
string filename = $"v{mapId}_r{row + 1}_c{col + 1}"; // <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>v1000_r1_c1.jpg
2025-06-15 20:14:45 +08:00
string spPath = PathUtil.GetMapTexure(1000, filename, "jpg");
GameObject obj = new GameObject(filename);
2025-06-16 00:15:41 +08:00
obj.transform.SetParent(mapRegion);
2025-06-15 20:14:45 +08:00
SpriteRenderer sr = obj.AddComponent<SpriteRenderer>();
float x = col * jpgscenew;
float y = (mapRownum - row - 1) * jpgscenew;
obj.transform.position = new Vector2(x, y);
2025-06-16 23:59:28 +08:00
_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}"; // <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>v1000_r1_c1.jpg
}
2025-06-15 20:14:45 +08:00
}
2025-06-16 23:59:28 +08:00
MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum);
2025-06-17 00:23:38 +08:00
UICellInfo.Instance.ShowMapCellInfo();
2025-06-16 23:59:28 +08:00
2025-06-15 20:14:45 +08:00
}
2025-06-16 00:15:41 +08:00
public void ClearMapRegions()
{
ClearMapGrid();
foreach (Transform child in mapRegion)
{
Destroy(child.gameObject);
}
}
2025-06-19 01:31:00 +08:00
public void CreateObs()
{
if(_curMapRegions == null) return;
float jpgscenew = PicMapPixel / 100;
MapManager.Instance.GenerateMap(jpgscenew * _curMapRegions.regionColNum, jpgscenew * _curMapRegions.regionRowNum, _curMapRegions.cellWidthPixel / 100.0f, _curMapRegions.cellHeightPixel / 100.0f);
}
2025-06-16 00:15:41 +08:00
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<Texture2D> loadASyncTexture2D(string fullPath,CancellationToken token)
{
return UniTask.RunOnThreadPool(async () =>
2025-06-15 20:14:45 +08:00
{
2025-06-16 00:15:41 +08:00
if (!File.Exists(fullPath))
2025-06-15 20:14:45 +08:00
{
2025-06-16 00:15:41 +08:00
Debug.LogError($"File not found: {fullPath}");
return null;
2025-06-15 20:14:45 +08:00
}
2025-06-16 00:15:41 +08:00
// <20><><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><ECB2BD>ȡ<EFBFBD>ļ<EFBFBD>
byte[] bytes = await File.ReadAllBytesAsync(fullPath, token);
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
await UniTask.Yield(PlayerLoopTiming.Update);
Texture2D texture = new Texture2D(2, 2); // <20><>ʼ<EFBFBD><CABC>С
if (texture.LoadImage(bytes))
{
return texture;
}
else
2025-06-15 20:14:45 +08:00
{
2025-06-16 00:15:41 +08:00
Debug.LogError($"Failed to load image from {fullPath}");
return null;
2025-06-15 20:14:45 +08:00
}
2025-06-16 00:15:41 +08:00
});
}
IEnumerator LoadSpriteT(string spName, GameObject go, SpriteRenderer sprite)
{
if (File.Exists(spName))
{
byte[] bytes = File.ReadAllBytes(spName);
Texture2D texture = new Texture2D(2, 2); // <20><>ʼ<EFBFBD><CABC>С<EFBFBD><D0A1><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD>
if (texture.LoadImage(bytes)) // <20><><EFBFBD><EFBFBD> JPG <20><> 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);
// }
//}
}
2025-06-14 13:46:24 +08:00
}