using HxGame.Data; using System; using System.IO; using UnityEngine; using UnityEngine.UI; using static UnityOpenFolder; ///创建地图 - 蓝色块 ///功能:创建地图, 删除地图, 自动贴图, 保存地形贴图 public class UICreateMap : MonoBehaviour { private const int REGIONWIDTHSCALL = 10; private const int REGIONHEIGHTSCALL = 10; public delegate void CreatedMapCallback(int mapId, int mapWidth, int mapHeight); public CreatedMapCallback onCreatedMapCallback; public InputField txtMapID; public InputField txtMapWidth; public InputField txtMapHeight; public InputField txtRegionWidth; public InputField txtRegionHeight; public InputField txtRegionName; public InputField txtStartNum; public Image MapBG; public Button btnCreateMap; //public Button btnDeleteMap; public Button btnAutoCreateRegion; public Button btnAutoCleanupRegion; private MapRegions _curMapRegions; private void Start() { _curMapRegions = new MapRegions(); txtStartNum.text = "0"; btnCreateMap.onClick.AddListener(CreateMap); //btnDeleteMap.onClick.AddListener(DeleteMap); btnAutoCreateRegion.onClick.AddListener(AutoLoadSprite); btnAutoCleanupRegion.onClick.AddListener(CleanupSprite); } bool CheckValid() { if (string.IsNullOrEmpty(txtMapID.text)) { UIWindow.Instance.ShowMessage("请填写地图ID"); return false; } //if (string.IsNullOrEmpty(txtMapWidth.text)) //{ // UIWindow.Instance.ShowMessage("请填写地图宽度"); // return false; //} //if (string.IsNullOrEmpty(txtMapHeight.text)) //{ // UIWindow.Instance.ShowMessage("请填写地图高度"); // return false; //} if (string.IsNullOrEmpty(txtRegionWidth.text)) { UIWindow.Instance.ShowMessage("请填写贴图宽度"); return false; } if (string.IsNullOrEmpty(txtRegionHeight.text)) { UIWindow.Instance.ShowMessage("请填写贴图高度"); return false; } int mapId = Convert.ToInt32(txtMapID.text); //int mapWidth = Convert.ToInt32(txtMapWidth.text); //int mapHeight = Convert.ToInt32(txtMapHeight.text); int regionWidth = Convert.ToInt32(txtRegionWidth.text); int regionHeight = Convert.ToInt32(txtRegionHeight.text); if (regionWidth <= 0 || regionHeight <= 0) { UIWindow.Instance.ShowMessage("请输入大于0的数值"); return false; } return true; } public void CreateMap() { if(!MapBG.name.Equals("BG")) { UIWindow.Instance.ShowMessage("请先关闭已有地图"); return; } if(UIWindow.Instance.uiMapPanel.HasMap(txtMapID.text)) { UIWindow.Instance.ShowMessage("已有该地图"); return; } if (!CheckValid()) return; //创建新地图文件夹 string path = $"{Application.dataPath}/GameAssets/Maps/{txtMapID.text}"; if(!Directory.Exists(path)) { Directory.CreateDirectory(path); //创建XML,Texture文件夹 Directory.CreateDirectory($"{path}/XML"); Directory.CreateDirectory($"{path}/Texture"); } OpenMap(); } private void OpenMap() { if (!MapBG.name.Equals("BG")) { UIWindow.Instance.ShowMessage("请先关闭已有地图"); return; } int mapId = Convert.ToInt32(txtMapID.text); int mapWidth = Convert.ToInt32(txtMapWidth.text); int mapHeight = Convert.ToInt32(txtMapHeight.text); MapBG.name = txtMapID.text; Vector2 size = new Vector2(mapWidth / MapManager.CELLSCALE, mapHeight / MapManager.CELLSCALE); MapBG.transform.GetComponent().sizeDelta = size; CreateImageRegion(); if (onCreatedMapCallback == null) return; onCreatedMapCallback(mapId, mapWidth, mapHeight); } public void CloseMap() { if (MapBG.name.Equals("BG")) { UIWindow.Instance.ShowMessage("还没有创建地图"); return; } Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions"); int count = regionsTrans.childCount; for (int i = 0; i < count; i++) { DestroyImmediate(regionsTrans.GetChild(0).gameObject); } MapBG.name = "BG"; MapBG.GetComponent().enabled = true; UIWindow.Instance.uiEditMapConfig.Cleanup(); } //public void DeleteMap() //{ // CloseMap(); //} private void CreateImageRegion() { if (!CheckValid()) return; Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions"); if (regionsTrans == null) { UIWindow.Instance.ShowMessage("没有找到Regions节点"); return; } UnityEngine.Object quadObj = Resources.Load("Prefabs/Region"); if (quadObj == null) { UIWindow.Instance.ShowMessage("Region.prefab失败"); return; } int mapWidth = Convert.ToInt32(txtMapWidth.text); int mapHeight = Convert.ToInt32(txtMapHeight.text); int regionWidth = Convert.ToInt32(txtRegionWidth.text); int regionHeight = Convert.ToInt32(txtRegionHeight.text); int row = mapWidth / MapManager.CELLSCALE / regionWidth; int col = mapHeight / MapManager.CELLSCALE / regionHeight; _curMapRegions.regions = new Region[row, col]; for (int i=0; i().anchoredPosition3D = new Vector3(i * regionWidth, j * regionHeight, 0); go.GetComponent().sizeDelta = new Vector2(0.9f * regionWidth, 0.9f * regionHeight); Button btn = go.GetComponent