Files
HX_MapEditor/Assets/Scripts/Map/MapManager.cs
tangbin 1cb6dc502f 添加
2025-06-22 15:21:25 +08:00

202 lines
5.8 KiB
C#

using HxGame;
using HxGame.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.UI;
public partial class MapManager : MonoBehaviour
{
//地图宽高缩放
public const int CELLSCALE = 100;
//格子宽像素
public const int widthPixel = 48;
//格子高像素
public const int heightPixel = 32;
//单张地图像素
public const int PicMapPixel = 512;
public delegate void LoadFinishedCallback();
public LoadFinishedCallback onLoadFinishedCallback;
//public delegate void CloseMapCallback();
//public CloseMapCallback onCloseMapCallback;
private static MapManager _mapManager = null;
public static MapManager Instance
{
get
{
if (_mapManager == null)
_mapManager = FindObjectOfType<MapManager>();
return _mapManager;
}
}
[HideInInspector]
public int mapWidth = 0;
[HideInInspector]
public int mapHeight = 0;
private int _maxWidth; //记录最大宽度索引
private int _maxHeight; //记录最大高度索引
private int _cellWidth;
private int _cellHeight;
private int _cellRows;
private int _cellCols;
//public CellNode[,] cellNodes;
public int _curOpenMapId;
private Material _cellMoveMat;
private Material _cellObsMat;
private Material _cellHideMat;
private Material _cellDefaultMat;
private Material _cellReturnMat; //回城点
private Material _cellReliveMat; //复活点
private Material _cellTeleportMat; //传送点
private Material _cellNpcMat; //npc位置
private Material _cellPathNodeMat; //pathNode
private Material _cellTriggerMat; //trigger
private bool _StartEditor;
private float _brushRadius;
private CellType _brushCellType;
public int CellRows { get { return _cellRows; } }
public int CellCols { get { return _cellCols; } }
public int CellWidth { get { return _cellWidth; } }
public int CellHeight { get { return _cellHeight; } }
public int MaxWidth { get { return _maxWidth; } }
public int MaxHeight { get { return _maxHeight; } }
private GameObject _curPathObj = null;
private void Start()
{
_cellMoveMat = Resources.Load<Material>("Materials/cellMoveMat");
_cellObsMat = Resources.Load<Material>("Materials/cellObsMat");
_cellHideMat = Resources.Load<Material>("Materials/cellHideMat");
_cellDefaultMat = Resources.Load<Material>("Materials/cellDefaultMat");
_cellReturnMat = Resources.Load<Material>("Materials/cellReturnMat");
_cellReliveMat = Resources.Load<Material>("Materials/cellReliveMat");
_cellTriggerMat = Resources.Load<Material>("Materials/cellTriggerMat");
_cellTeleportMat = Resources.Load<Material>("Materials/cellTeleportMat");
_cellNpcMat = Resources.Load<Material>("Materials/cellNpcMat");
_cellPathNodeMat = Resources.Load<Material>("Materials/cellPathNodeMat");
Cleanup();
}
private void OnCreatedMap(int mapId, int mapWidth, int mapHeight)
{
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
_curOpenMapId = mapId;
}
public void StartEditor()
{
if (_curOpenMapId < 0)
{
UIWindow.Instance.ShowMessage("请先打开地图");
return;
}
_StartEditor = true;
}
public void CloseEditor()
{
_StartEditor = false;
}
public void SetBrush(float radius, CellType type)
{
_brushRadius = radius;
_brushCellType = type;
}
List<MapConfig.AreaConfig> tempSafeAreasConfig = new List<MapConfig.AreaConfig>();
public void CloseMap()
{
if (_curOpenMapId <= 0)
return;
UIWindow.Instance.uiCellInfo.CloseMap();
UIWindow.Instance.uiMonstersPanel.RemoveAll();
ClearMapRegions();
Cleanup();
}
private void Cleanup()
{
mapWidth = 0;
mapHeight = 0;
_maxWidth = 0;
_maxHeight = 0;
_cellWidth = -1;
_cellHeight = -1;
_cellRows = -1;
_cellCols = -1;
_curOpenMapId = -1;
_StartEditor = false;
_brushRadius = 0;
_brushCellType = CellType.None;
}
private void Update()
{
bool bHoverUI = EventSystem.current != null && EventSystem.current.IsPointerOverGameObject();
if (bHoverUI)
return;
UpdateCamera();
UpdateCellPos();
}
public void LoadSprite(string spName, GameObject go, int regionWidth=0, int regionHeight=0)
{
StartCoroutine(LoadSpriteCor(spName, go, regionWidth, regionHeight));
}
IEnumerator LoadSpriteCor(string spName, GameObject go, int regionWidth, int regionHeight)
{
string spPath = PathUtil.GetTexture(_curOpenMapId, spName, "jpg");
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(spPath))
{
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.ConnectionError)
{
UIWindow.Instance.ShowMessage(req.error);
}
else
{
Texture2D tt = DownloadHandlerTexture.GetContent(req);
if (tt == null)
yield break;
//根据获取的Texture建立一个sprite
//Sprite sprite = Sprite.Create((Texture2D)tt, new Rect(0, 0, tt.width, tt.height), new Vector2(0, 0));
//sprite.name = spName;
//go.GetComponent<Image>().sprite = sprite;
tt.name = spName;
go.GetComponent<RawImage>().texture = tt;
go.GetComponent<RectTransform>().sizeDelta = new Vector2(regionWidth, regionHeight);
}
}
}
}