396 lines
12 KiB
C#
396 lines
12 KiB
C#
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 (mapWidth <= 0 || mapHeight <= 0 || 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<RectTransform>().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<Image>().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<row; i++)
|
|
{
|
|
for(int j=0; j<col; j++)
|
|
{
|
|
_curMapRegions.regions[i, j] = new Region();
|
|
GameObject go = Instantiate(quadObj) as GameObject;
|
|
go.name = $"{i}_{j}";
|
|
go.transform.SetParent(regionsTrans, false);
|
|
go.layer = LayerMask.NameToLayer("MapCell");
|
|
go.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(i * regionWidth, j * regionHeight, 0);
|
|
go.GetComponent<RectTransform>().sizeDelta = new Vector2(0.9f * regionWidth, 0.9f * regionHeight);
|
|
Button btn = go.GetComponent<Button>();
|
|
btn.onClick.AddListener(() => OpenImageRegion(go));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OpenImageRegion(GameObject go)
|
|
{
|
|
var extension = new[]
|
|
{
|
|
new ExtensionFilter("Image Files", "png", "jpg", "jpeg")
|
|
};
|
|
|
|
string[] path = UnityOpenFolder.OpenFilePanel("Open File", "", extension, false);
|
|
|
|
if (path.Length == 0)
|
|
return;
|
|
|
|
int regionWidth = Convert.ToInt32(txtRegionWidth.text);
|
|
int regionHeight = Convert.ToInt32(txtRegionHeight.text);
|
|
|
|
int idx = path[0].LastIndexOf('\\');
|
|
string tmpName = path[0].Substring(idx + 1);
|
|
tmpName = tmpName.Split('.')[0];
|
|
|
|
MapManager.Instance.LoadSprite(tmpName, go, regionWidth, regionHeight);
|
|
}
|
|
|
|
|
|
|
|
public void AutoLoadSprite()
|
|
{
|
|
if (!CheckValid())
|
|
return;
|
|
|
|
if (string.IsNullOrEmpty(txtStartNum.text))
|
|
{
|
|
UIWindow.Instance.ShowMessage("请填写贴图起始编号");
|
|
return ;
|
|
}
|
|
|
|
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);
|
|
|
|
int rows = mapWidth / MapManager.CELLSCALE / regionWidth;
|
|
int cols = mapHeight / MapManager.CELLSCALE / regionHeight;
|
|
|
|
int startNum = Convert.ToInt32(txtStartNum.text);
|
|
int num = 0;
|
|
|
|
Transform imgTrans = UIWindow.Instance.mapTrans.Find("Regions");
|
|
|
|
for (int i=0; i< rows; i++)
|
|
{
|
|
for(int j=0; j< cols; j++)
|
|
{
|
|
string tmpName = $"{i}_{j}";
|
|
Transform trans = imgTrans.Find(tmpName);
|
|
|
|
if (trans == null)
|
|
continue;
|
|
|
|
//num = startNum + (col - (j+1)) * row + i;
|
|
//tmpName = string.Format("{0}_{1:D2}", txtRegionName.text, num);
|
|
|
|
tmpName = _curMapRegions.regions[i, j].regionName;
|
|
if(string.IsNullOrEmpty(tmpName))
|
|
{
|
|
num = startNum + (cols - (j+1)) * rows + i;
|
|
tmpName = string.Format("{0}_{1:D2}", txtRegionName.text, num);
|
|
}
|
|
|
|
MapManager.Instance.LoadSprite(tmpName, trans.gameObject, regionWidth, regionHeight);
|
|
|
|
}
|
|
}
|
|
|
|
MapBG.GetComponent<Image>().enabled = false;
|
|
}
|
|
|
|
public void CleanupSprite()
|
|
{
|
|
Transform imgTrans = UIWindow.Instance.mapTrans.Find("Regions");
|
|
for(int i=0; i<imgTrans.childCount; i++)
|
|
{
|
|
//Image image = imgTrans.GetChild(i).GetComponent<Image>();
|
|
//image.sprite = null;
|
|
RawImage image = imgTrans.GetChild(i).GetComponent<RawImage>();
|
|
image.texture = null;
|
|
}
|
|
|
|
btnAutoCreateRegion.gameObject.SetActive(true);
|
|
btnAutoCleanupRegion.gameObject.SetActive(false);
|
|
}
|
|
|
|
public void SaveRegions()
|
|
{
|
|
if (!CheckValid())
|
|
return;
|
|
|
|
Transform regionsTrans = UIWindow.Instance.mapTrans.Find("Regions");
|
|
if (regionsTrans == null)
|
|
{
|
|
UIWindow.Instance.ShowMessage("没有找到Regions节点");
|
|
return;
|
|
}
|
|
|
|
MapRegions mapRegions = new MapRegions();
|
|
mapRegions.mapWidth = Convert.ToInt32(txtMapWidth.text);
|
|
mapRegions.mapHeight = Convert.ToInt32(txtMapHeight.text);
|
|
mapRegions.regionWidth = Convert.ToInt32(txtRegionWidth.text);
|
|
mapRegions.regionHeight = Convert.ToInt32(txtRegionHeight.text);
|
|
|
|
int row = mapRegions.mapWidth / MapManager.CELLSCALE / mapRegions.regionWidth;
|
|
int col = mapRegions.mapHeight / MapManager.CELLSCALE / mapRegions.regionHeight;
|
|
|
|
Transform trans = null;
|
|
mapRegions.regions = new Region[row, col];
|
|
for (int i = 0; i < row; i++)
|
|
{
|
|
for (int j = 0; j < col; j++)
|
|
{
|
|
mapRegions.regions[i, j] = new Region();
|
|
mapRegions.regions[i, j].indexX = i;
|
|
mapRegions.regions[i, j].indexY = j;
|
|
|
|
string regionName = $"{i}_{j}";
|
|
trans = regionsTrans.Find(regionName);
|
|
if(trans == null)
|
|
{
|
|
UIWindow.Instance.ShowMessage($"没有找到region节点 name={regionName}");
|
|
return;
|
|
}
|
|
|
|
//mapRegions.regions[i, j].regionName = trans.GetComponent<Image>().sprite.name;
|
|
mapRegions.regions[i, j].regionName = trans.GetComponent<RawImage>().texture.name;
|
|
}
|
|
}
|
|
|
|
mapRegions.SaveXML(Convert.ToInt32(txtMapID.text));
|
|
}
|
|
|
|
public void LoadMapRegions(int mapId)
|
|
{
|
|
//MapRegions mapRegions = new MapRegions();
|
|
//if (!mapRegions.LoadXML(mapId))
|
|
// return;
|
|
if (!_curMapRegions.LoadXML(mapId))
|
|
return;
|
|
|
|
txtMapID.text = mapId.ToString();
|
|
txtMapWidth.text = _curMapRegions.mapWidth.ToString();
|
|
txtMapHeight.text = _curMapRegions.mapHeight.ToString();
|
|
txtRegionWidth.text = _curMapRegions.regionWidth.ToString();
|
|
txtRegionHeight.text = _curMapRegions.regionHeight.ToString();
|
|
|
|
txtRegionName.text = _curMapRegions.regions[0, 0].regionName.Split('_')[0];
|
|
int idx = 1000;
|
|
int tmpIdx = 1000;
|
|
foreach (var region in _curMapRegions.regions)
|
|
{
|
|
var splitNames = region.regionName.Split('_');
|
|
if (splitNames.Length != 2)
|
|
continue;
|
|
|
|
tmpIdx = Convert.ToInt32(splitNames[1]);
|
|
if (tmpIdx < idx)
|
|
idx = tmpIdx;
|
|
}
|
|
|
|
txtStartNum.text = idx.ToString();
|
|
|
|
OpenMap();
|
|
AutoLoadSprite();
|
|
}
|
|
}
|