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; using UnityEngine.UIElements; public partial class MapManager : MonoBehaviour { //地图宽高缩放 public const int CELLSCALE = 100; //格子宽像素 public const int widthPixel = 64; //格子高像素 public const int heightPixel = 32; //单张地图像素 public int PicMapPixel => int.Parse(mapPic.text); public delegate void LoadFinishedCallback(); public LoadFinishedCallback onLoadFinishedCallback; //public delegate void CloseMapCallback(); //public CloseMapCallback onCloseMapCallback; public InputField mapPic; private static MapManager _mapManager = null; public static MapManager Instance { get { if (_mapManager == null) _mapManager = FindObjectOfType(); 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("Materials/cellMoveMat"); _cellObsMat = Resources.Load("Materials/cellObsMat"); _cellHideMat = Resources.Load("Materials/cellHideMat"); _cellDefaultMat = Resources.Load("Materials/cellDefaultMat"); _cellReturnMat = Resources.Load("Materials/cellReturnMat"); _cellReliveMat = Resources.Load("Materials/cellReliveMat"); _cellTriggerMat = Resources.Load("Materials/cellTriggerMat"); _cellTeleportMat = Resources.Load("Materials/cellTeleportMat"); _cellNpcMat = Resources.Load("Materials/cellNpcMat"); _cellPathNodeMat = Resources.Load("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 tempSafeAreasConfig = new List(); public void CloseMap() { if (_curOpenMapId <= 0) return; //清理所有场景的东西 foreach (Transform child in mapMonsterArea) { Destroy(child.gameObject); } foreach (Transform child in mapNPCArea) { Destroy(child.gameObject); } foreach (Transform child in mapTeleportArea) { Destroy(child.gameObject); } foreach (Transform child in mapReliveArea) { Destroy(child.gameObject); } UIWindow.Instance.uiCellInfo.CloseMap(); 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; UIWindow.Instance.uiEditMapConfig.Cleanup(); } 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().sprite = sprite; tt.name = spName; go.GetComponent().texture = tt; go.GetComponent().sizeDelta = new Vector2(regionWidth, regionHeight); } } } }