Example #1
0
    private static void Json2Map()
    {
        GameObject  map         = GameObject.FindGameObjectWithTag("Map");
        MetaTileMap metaTileMap = map.GetComponentInChildren <MetaTileMap>();

        metaTileMap.ClearAllTiles();

        TilemapConverter converter = new TilemapConverter();

        TileMapBuilder builder = new TileMapBuilder(metaTileMap, true);

        Dictionary <string, TilemapLayer> layers = DeserializeJson();

        List <Tuple <Vector3Int, ObjectTile> > objects = new List <Tuple <Vector3Int, ObjectTile> >();

        foreach (KeyValuePair <string, TilemapLayer> layer in layers)
        {
            List <Vector3Int> positions =
                layer.Value.TilePositions.ConvertAll(coord => new Vector3Int(coord.X, coord.Y, 0));

            for (int i = 0; i < positions.Count; i++)
            {
                Vector3Int  position = positions[i];
                GenericTile tile     = converter.DataToTile(layer.Value.Tiles[i]);

                if (tile is ObjectTile)
                {
                    if (!objects.Exists(t => t.Item1.Equals(position) && t.Item2 == tile))
                    {
                        objects.Add(new Tuple <Vector3Int, ObjectTile>(position, (ObjectTile)tile));
                    }
                }
                else
                {
                    builder.PlaceTile(position, tile);
                }
            }
        }

        foreach (Tuple <Vector3Int, ObjectTile> tuple in objects)
        {
            Vector3Int position = tuple.Item1;
            ObjectTile obj      = tuple.Item2;

            Matrix4x4 matrix = obj.Rotatable ? FindObjectPosition(metaTileMap, ref position, obj) : Matrix4x4.identity;

            builder.PlaceTile(position, obj, matrix);
        }

        // mark as dirty, otherwise the scene can't be saved.
        EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
        Logger.Log("Import kinda finished");
    }