public void LoadMap(string filePath, string dataPath) { if (!File.Exists(filePath) || !File.Exists(dataPath)) { return; } string json = File.ReadAllText(filePath); JsonMapData jsonMapData = JsonUtility.FromJson <JsonMapData>(json); byte[] bufferData = File.ReadAllBytes(dataPath); Texture2D textureData = new Texture2D(1, 1); textureData.LoadImage(bufferData); Color32[] pixelsData = textureData.GetPixels32(); for (int y = 0; y < jsonMapData.sizeMap.y; y++) { for (int x = 0; x < jsonMapData.sizeMap.x; x++) { int index = y * jsonMapData.sizeMap.x + x; JsonTileData tile = new JsonTileData(); tile.indexMap = index; tile.tileLayer.layer1 = pixelsData[index].r; tile.tileLayer.layer2 = pixelsData[index].g; tile.tileLayer.layer3 = pixelsData[index].b; tile.tileLayer.layer4 = pixelsData[index].a; jsonMapData.tileData[index] = tile; } } if (currentMap != null) { Debug.Log("Existing map"); Destroy(currentMap.gameObject); } GameObject inst = new GameObject("MapDataModif"); inst.transform.parent = this.transform; currentMap = inst.AddComponent <MapDataMof>(); Material materialSelected = null; switch (mTypeMap) { case 1: materialSelected = tilesetMat; break; case 2: materialSelected = chunckMat; break; case 3: materialSelected = fullMat; break; default: break; } //currentMap.CreateMap(new Vector2Int(jsonMapData.sizeMap.x, jsonMapData.sizeMap.y), tilesetMat); currentMap.CreateEditorMap(new Vector2Int(jsonMapData.sizeMap.x, jsonMapData.sizeMap.y), mTypeMap, materialSelected); currentMap.LoadMap(jsonMapData); CenterMap(); UpdateToolsButtons(); MenuManager.Instance.PreviousPanel(); }
public void CreateNewMap() { bool isChecked = true; if (!inpSizeX.Validate()) { isChecked = false; } if (!inpSizeY.Validate()) { isChecked = false; } if (isChecked) { if (currentMap != null) { Debug.Log("Existing map"); Destroy(currentMap.gameObject); } GameObject inst = new GameObject("MapDataModif"); inst.transform.parent = this.transform; currentMap = inst.AddComponent <MapDataMof>(); currentMap.isMapEditor = true; int sizeX = 1; int sizeY = 1; int.TryParse(inpSizeX.Content, out sizeX); int.TryParse(inpSizeY.Content, out sizeY); if (mTypeMap == 2) { Debug.Log("NotDoneYet"); } Material materialSelected = null; TilesetManager.Tileset tilesetTarget = null; switch (mTypeMap) { case 1: materialSelected = tilesetMat; tilesetTarget = tilesetTile; break; case 2: materialSelected = chunckMat; tilesetTarget = tilesetChunck; break; case 3: materialSelected = fullMat; tilesetTarget = tilesetFull; break; default: break; } //currentMap.CreateMap(new Vector2Int(sizeX, sizeY), tilesetMat); TilesetManager.instance.SetTileset(0, tilesetTarget); currentMap.CreateEditorMap(new Vector2Int(sizeX, sizeY), mTypeMap, materialSelected); MenuManager.Instance.ActiveState(EMenuState.Editor_Menu); UpdateToolsButtons(); CenterMap(); } }