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

@@ -557,7 +557,7 @@ public partial class MapManager : MonoBehaviour
if (cellType < EditCellType.AudioTrigger)
obj = _cellAsset;
obj = null;
else if (cellType == EditCellType.AudioTrigger)
obj = Resources.Load("Prefabs/audioTriggerCenterPoint");
else if (cellType == EditCellType.TriggerCell)
@@ -714,26 +714,6 @@ public partial class MapManager : MonoBehaviour
SetGameObjectActiveWithAllChild(cellsTrans, true);
return;
}
for (int y = 0; y < _cellRows; y++)
{
for (int x = 0; x < _cellCols; x++)
{
CellNode cell = GetCell(x, y);
GameObject go = Instantiate(_cellAsset) as GameObject;
go.name = $"{x}_{y}"; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD><5F>
go.transform.SetParent(cellsTrans, false);
go.transform.localScale = new Vector3(((float)_cellWidth / CELLSCALE) - 0.1f, ((float)_cellHeight / CELLSCALE) - 0.1f, 1);
//2d <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Vector3 pos = cell.GetPos((float)_cellWidth / CELLSCALE, (float)_cellHeight / CELLSCALE);
go.transform.localPosition = new Vector3(pos.x, pos.z, pos.y);
go.layer = LayerMask.NameToLayer("MapCell");
ShowCellType(go, cell.cellType);
AddLayerCell(go, (CellLayer)cell.cellType);
}
}
}
public void HideCells()

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();
}
}

View File

@@ -14,13 +14,9 @@ public partial class MapManager : MonoBehaviour
public event Action onMapCreated;
public CellType cellType;
public Map GenerateMap(float width, float height,float sideWidth, float sideHeight)
public Map GenerateMap(float width, float height, float sideWidth, float sideHeight)
{
if (map != null)
{
map.Release();
map = null;
}
ClearMapGrid();
map = new Map(width, height, sideWidth, sideHeight);
map.SetMapId(1000);
//默认设置半透明0.3
@@ -28,6 +24,25 @@ public partial class MapManager : MonoBehaviour
onMapCreated?.Invoke();
return map;
}
public void ClearMapGrid()
{
if (map != null)
{
map.Release();
map = null;
}
}
public void HideMapGrid()
{
if(map != null && map.mapGrid != null)
map.mapGrid.gameObject.SetActive(false);
}
public void ShowMapGrid()
{
if (map != null && map.mapGrid != null)
map.mapGrid.gameObject.SetActive(true);
}
public void ChangeGridAlpha(float alpha)
{
if (map == null)

View File

@@ -47,7 +47,7 @@ public partial class MapManager : MonoBehaviour
private int _cellCols;
//public CellNode[,] cellNodes;
private int _curOpenMapId;
public int _curOpenMapId;
private Material _cellMoveMat;
private Material _cellObsMat;
@@ -73,24 +73,10 @@ public partial class MapManager : MonoBehaviour
public int MaxWidth { get { return _maxWidth; } }
public int MaxHeight { get { return _maxHeight; } }
private UnityEngine.Object _cellAsset = null;
private GameObject _curPathObj = null;
private void Start()
{
_cellAsset = Resources.Load("Prefabs/Cell");
if (_cellAsset == null)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>cell.prefabʧ<62><CAA7>");
return;
}
foreach (CellLayer layer in Enum.GetValues(typeof(CellLayer)))
{
List<GameObject> gos = new List<GameObject>();
_layCellsMap.Add(layer, gos);
}
_cellMoveMat = Resources.Load<Material>("Materials/cellMoveMat");
_cellObsMat = Resources.Load<Material>("Materials/cellObsMat");
_cellHideMat = Resources.Load<Material>("Materials/cellHideMat");
@@ -102,7 +88,6 @@ public partial class MapManager : MonoBehaviour
_cellTeleportMat = Resources.Load<Material>("Materials/cellTeleportMat");
_cellNpcMat = Resources.Load<Material>("Materials/cellNpcMat");
_cellPathNodeMat = Resources.Load<Material>("Materials/cellPathNodeMat");
UIWindow.Instance.uiCreateMap.onCreatedMapCallback += OnCreatedMap;
Cleanup();
}
@@ -122,7 +107,6 @@ public partial class MapManager : MonoBehaviour
}
_StartEditor = true;
_curEditCellType = EditCellType.None;
}
public void CloseEditor()
@@ -136,80 +120,15 @@ public partial class MapManager : MonoBehaviour
_brushCellType = type;
}
public void SaveCellsXml()
{
if(cellsNode.Length == 0)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>");
return;
}
string path = PathUtil.GetXmlPath(_curOpenMapId, "Obs");
if (File.Exists(path))
File.Delete(path);
XmlDocument xml = new XmlDocument();
XmlDeclaration node = xml.CreateXmlDeclaration("1.0", "utf-8", "no");
xml.AppendChild(node);
XmlElement Root = xml.CreateElement("Item");
xml.AppendChild(Root);
Root.SetAttribute("ID", _curOpenMapId.ToString());
Root.SetAttribute("MapWidth", mapWidth.ToString());
Root.SetAttribute("MapHeight", mapHeight.ToString());
Root.SetAttribute("CellWidth", _cellWidth.ToString());
Root.SetAttribute("CellHeight", _cellHeight.ToString());
CellNode cell = null;
StringBuilder sb = new StringBuilder();
//tempSafeAreasConfig.Clear();
for (int row = 0; row < _cellRows; row++)
{
for (int col = 0; col < _cellCols; col++)
{
cell = GetCell(col, row);
if(cell == null)
{
UIWindow.Instance.ShowMessage("<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD>ϰ<EFBFBD>ȫ<EFBFBD><C8AB>ö<EFBFBD><C3B6>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//if (CheckIsSafeCell(cell))
//{
// cell.cellType |= CellType.Safe;
//}
sb.Append(string.Format($"{row},{col},{(int)cell.cellType};"));
}
}
sb.Remove(sb.Length - 1, 1);
string Value = sb.ToString();
Root.SetAttribute("Value", Value);
xml.Save(path);
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>xml<6D>ɹ<EFBFBD>.");
}
List<MapConfig.AreaConfig> tempSafeAreasConfig = new List<MapConfig.AreaConfig>();
public void CloseMap()
{
if (_curOpenMapId <= 0)
return;
RemoveAllCells();
UIWindow.Instance.uiCellInfo.CloseMap();
UIWindow.Instance.uiCreateMap.CloseMap();
UIWindow.Instance.uiMonstersPanel.RemoveAll();
ClearMapRegions();
Cleanup();
}
@@ -228,7 +147,6 @@ public partial class MapManager : MonoBehaviour
_StartEditor = false;
_brushRadius = 0;
_brushCellType = CellType.None;
cellsNode = null;
}
@@ -238,175 +156,8 @@ public partial class MapManager : MonoBehaviour
if (bHoverUI)
return;
UpdateCamera();
if (_curEditCellType != EditCellType.None)
{
EditSpecialMapPoint(_curEditCellType);
return;
}
if (!_StartEditor)
return;
if (Input.GetMouseButton(0))
{
EditCell();
}
}
private void EditCell()
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
GameObject gameObj = hitInfo.collider.gameObject;
if (gameObj.layer != LayerMask.NameToLayer("MapCell"))
return;
if (_brushRadius == 0)
{
CellType ct = SetCellType(gameObj.name, _brushCellType);
ShowCellType(gameObj, ct);
return;
}
Collider[] hits = Physics.OverlapSphere(gameObj.transform.position, _brushRadius);// LayerMask.NameToLayer("MapCell")
if (hits.Length > 0)
{
for (int i = 0; i < hits.Length; i++)
{
CellType ct = SetCellType(hits[i].name, _brushCellType);
ShowCellType(hits[i].gameObject, ct);
}
}
}
}
private void EditSpecialMapPoint(EditCellType type)
{
if (Input.GetMouseButtonUp(0))
{
if(type == EditCellType.NpcPath)
{
if (_curPathObj == null)
return;
UIWindow.Instance.uiNpcsPanel.curActiveList.Add(_curPathObj.name);
UIWindow.Instance.uiNpcsPanel.curActiveInput.text += $"[{_curPathObj.name.Replace('_', ',')}]";
AddNpcPathSize();
_curPathObj = null;
}
else if (type == EditCellType.MonsterPath)
{
if (_curPathObj == null)
return;
//UIWindow.Instance.uiMonstersPanel_old.curActiveList.Add(_curPathObj.name);
//UIWindow.Instance.uiMonstersPanel_old.curActiveInput.text += $"[{_curPathObj.name.Replace('_', ',')}]";
AddMonsterPathSize();
_curPathObj = null;
}
else
{
_curEditCellType = EditCellType.None;
return;
}
}
if(type == EditCellType.NpcPath)
{
if(Input.GetKeyDown(KeyCode.Escape))
{
string last = $"NpcPath{_curNpcPointIdx}{GetCurNpcPathIdx()}";
GameObject lastObj = GameObject.Find(last);
DestroyImmediate(lastObj);
_curEditCellType = EditCellType.None;
return;
}
}
else if (type == EditCellType.MonsterPath)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
string last = $"MonsterPath{_curMonsterPoint.areaIdx}{GetCurMonsterPathIdx()}";
GameObject lastObj = GameObject.Find(last);
DestroyImmediate(lastObj);
_curEditCellType = EditCellType.None;
return;
}
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
GameObject gameObj = hitInfo.collider.gameObject;
if (gameObj.layer != LayerMask.NameToLayer("MapCell"))
return;
string[] point = gameObj.name.Split('_');
if (point.Length != 2)
{
if (Input.GetMouseButtonUp(1))
{
int xx = (int)((gameObj.transform.localPosition.x - _cellWidth / 100.0f / 2) / (_cellWidth / 100.0f));
int yy = (int)((gameObj.transform.localPosition.y - _cellHeight / 100.0f / 2) / (_cellHeight / 100.0f));
UIWindow.Instance.uiNpcsPanel.curActiveList.Remove($"{xx}_{yy}");
DestroyImmediate(gameObj);
}
return;
}
int x, y;
x = Convert.ToInt32(point[0]); //<2F><>
y = Convert.ToInt32(point[1]); //<2F><>
CreateSpecialPoint(x, y, type);
_curPathObj = gameObj;
switch (type)
{
case EditCellType.ReturnCell:
UIWindow.Instance.uiEditMapConfig.txtReturnMapCellPoint.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.TeleportCell:
UIWindow.Instance.uiTeleportPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.NpcCell:
UIWindow.Instance.uiNpcsPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.PathNodeCell:
UIWindow.Instance.uiCellEditor.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.TriggerCell:
UIWindow.Instance.uiTriggersPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.AudioTrigger:
UIWindow.Instance.uiAudioTriggerPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.ReliveCell:
UIWindow.Instance.uiRelivesPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.SellArea:
UIWindow.Instance.uiSellAreasPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.MonsterArea:
UIWindow.Instance.uiMonstersPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.FuBenArea:
UIWindow.Instance.uiFuBensPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
case EditCellType.JuBaoArea:
UIWindow.Instance.uiJuBaosPanel.curActiveInput.text = gameObj.name.Replace('_', ',');
break;
}
}
}
public void LoadSprite(string spName, GameObject go, int regionWidth=0, int regionHeight=0)
{