第一次提交
This commit is contained in:
395
Assets/Scripts/UI/UICreateMap.cs
Normal file
395
Assets/Scripts/UI/UICreateMap.cs
Normal file
@@ -0,0 +1,395 @@
|
||||
using HxGame.Data;
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using static UnityOpenFolder;
|
||||
|
||||
///<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ - <20><>ɫ<EFBFBD><C9AB>
|
||||
///<2F><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC> ɾ<><C9BE><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC> <20>Զ<EFBFBD><D4B6><EFBFBD>ͼ<EFBFBD><CDBC> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
||||
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("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼID");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(txtMapWidth.text))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(txtMapHeight.text))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼ<EFBFBD>߶<EFBFBD>");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(txtRegionWidth.text))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(txtRegionHeight.text))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼ<EFBFBD>߶<EFBFBD>");
|
||||
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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>ֵ");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void CreateMap()
|
||||
{
|
||||
if(!MapBG.name.Equals("BG"))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȹر<C8B9><D8B1><EFBFBD><EFBFBD>е<EFBFBD>ͼ");
|
||||
return;
|
||||
}
|
||||
|
||||
if(UIWindow.Instance.uiMapPanel.HasMap(txtMapID.text))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD>иõ<D0B8>ͼ");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CheckValid())
|
||||
return;
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>ͼ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
string path = $"{Application.dataPath}/GameAssets/Maps/{txtMapID.text}";
|
||||
if(!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>XML,Texture<72>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
Directory.CreateDirectory($"{path}/XML");
|
||||
Directory.CreateDirectory($"{path}/Texture");
|
||||
}
|
||||
|
||||
OpenMap();
|
||||
}
|
||||
|
||||
private void OpenMap()
|
||||
{
|
||||
if (!MapBG.name.Equals("BG"))
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȹر<C8B9><D8B1><EFBFBD><EFBFBD>е<EFBFBD>ͼ");
|
||||
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("<22><>û<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ͼ");
|
||||
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("û<><C3BB><EFBFBD>ҵ<EFBFBD>Regions<6E>ڵ<EFBFBD>");
|
||||
return;
|
||||
}
|
||||
|
||||
UnityEngine.Object quadObj = Resources.Load("Prefabs/Region");
|
||||
if (quadObj == null)
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("Region.prefabʧ<62><CAA7>");
|
||||
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("<22><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ͼ<EFBFBD><CDBC>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>");
|
||||
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("û<><C3BB><EFBFBD>ҵ<EFBFBD>Regions<6E>ڵ<EFBFBD>");
|
||||
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($"û<><C3BB><EFBFBD>ҵ<EFBFBD>region<6F>ڵ<EFBFBD> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user