This commit is contained in:
2025-06-16 00:15:41 +08:00
parent bcfa5ce1ec
commit 6d79a5baa1
16 changed files with 246 additions and 3838 deletions

View File

@@ -5,6 +5,8 @@ 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;
@@ -12,10 +14,14 @@ 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<string, (int maxRow, int maxCol)> allMaps = new Dictionary<string, (int maxRow, int maxCol)>();
public bool LoadObsXml()
{
if (_curOpenMapId < 0)
@@ -83,51 +89,119 @@ public partial class MapManager : MonoBehaviour
}
return true;
}
public void LoadMapSprite()
public void LoadMapRegions(int mapId)
{
//map<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
GameObject map = new GameObject("map");
int mapRownum = 11;
int mapColumn = 14;
_curOpenMapId = mapId;
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;
//ƽ<><C6BD>;
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++)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>v1000_r1_c1.jpg
string filename = $"v{1000}_r{row + 1}_c{col + 1}";
string filename = $"v{mapId}_r{row + 1}_c{col + 1}"; // <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>v1000_r1_c1.jpg
string spPath = PathUtil.GetMapTexure(1000, filename, "jpg");
GameObject obj = new GameObject(filename);
obj.transform.SetParent(map.transform);
obj.transform.SetParent(mapRegion);
SpriteRenderer sr = obj.AddComponent<SpriteRenderer>();
float x = col * jpgscenew;
float y = (mapRownum - row - 1) * jpgscenew;
obj.transform.position = new Vector2(x, y);
StartCoroutine(LoadSpriteT(spPath, obj,sr));
multithreadLoadTextrue(spPath,sr);
}
}
MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum);
MapManager.Instance.GenerateMap(jpgscenew * mapColumn, jpgscenew * mapRownum, 0.48f, 0.32f);
}
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<Texture2D> loadASyncTexture2D(string fullPath,CancellationToken token)
{
return UniTask.RunOnThreadPool(async () =>
{
if (!File.Exists(fullPath))
{
Debug.LogError($"File not found: {fullPath}");
return null;
}
// <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
{
Debug.LogError($"Failed to load image from {fullPath}");
return null;
}
});
}
IEnumerator LoadSpriteT(string spName, GameObject go, SpriteRenderer sprite)
{
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
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
{
UIWindow.Instance.ShowMessage(req.error);
}
}
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();
}
}