private static string convertWorldTileGridArrayToJSON(WorldTileMap world) { WorldJSONWrapper jsonWrapper = new WorldJSONWrapper(); GameObject[,] gridArray = world.grid; int width = (int)world.width; int height = (int)world.height; TileListJSONWrapper <TileJSONWrapper> tileList = new TileListJSONWrapper <TileJSONWrapper>(); tileList.list = new List <TileJSONWrapper>(); for (var i = 0; i < width; ++i) { for (var j = 0; j < height; ++j) { GridTile tileComponent = gridArray[i, j].GetComponent <GridTile>(); Debug.Log(tileComponent.currentTileType); TileJSONWrapper tileWrapper = new TileJSONWrapper(tileComponent.currentTileType, i, j); tileList.list.Add(tileWrapper); } } jsonWrapper.width = width; jsonWrapper.height = height; jsonWrapper.tileList = tileList.list; return(JsonUtility.ToJson(jsonWrapper)); }
public static void saveMapAsJSON(WorldTileMap worldMap, string mapName) { if (worldMap != null) { WorldTileMap map = worldMap; string jsonData = ""; jsonData = convertWorldTileGridArrayToJSON(map); mapName = mapName == null || mapName == "" ? "levelData" : mapName; string filePath = completeFilePath + mapName; File.Create(filePath + ".json").Close(); File.WriteAllText(filePath + ".json", jsonData); } }
private static WorldJSONWrapper loadLevelFromJSONData(WorldTileMap worldMap, string levelName) { string levelFilePath = levelDataResourceFolder; TextAsset targetFile; if (levelName == null || levelName == "") { levelFilePath = gameDataProjectFilePath.Replace(".json", ""); } else { levelFilePath += levelName.Replace(".json", ""); } Debug.Log(levelFilePath); targetFile = Resources.Load <TextAsset>(levelFilePath); return(wrapMapFromJSON(targetFile.text, worldMap)); }
private static WorldJSONWrapper wrapMapFromJSON(string json, WorldTileMap worldMap) { WorldJSONWrapper newMapWrapper = (WorldJSONWrapper)JsonUtility.FromJson(json, System.Type.GetType("WorldJsonUtility").GetNestedType("WorldJSONWrapper")); return(newMapWrapper); }
public static WorldJSONWrapper loadLevelData(WorldTileMap worldMap) { return(loadLevelFromJSONData(worldMap, null)); }
public static WorldJSONWrapper loadLevelData(WorldTileMap worldMap, string levelName) { return(loadLevelFromJSONData(worldMap, levelName)); }
//TODO: Properly handle overwriting levels? public static void saveMapAsJSON(WorldTileMap worldMap) { saveMapAsJSON(worldMap, null); }