public void LoadMap(string filePath, string dataPath, bool isLocalSave = true) { if (isLocalSave) { 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; } } GenrateMap(jsonMapData); } else { DataBaseManager.Instance.DownloadSave(filePath, new DataBaseManager.OnTextLoaded(MapDownloaded), null); } }
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(); }