예제 #1
0
        static bool ImportTileLayer(TMX.Layer layer, GameObject newGrid, int layerID, ImportedTileset[] importedTilesets, ITilemapImportOperation[] importOperations,
                                    TilemapRenderer.SortOrder tileSortOrder, int cellWidth, int cellHeight, bool infinite)
        {
            if (infinite != (layer.data.chunks != null))
            {
                Debug.LogError("Our map infinite setting is " + infinite + " but our chunks value is " + layer.data.chunks);
                return(false);
            }
            GameObject newLayer = new GameObject(layer.name, typeof(Tilemap), typeof(TilemapRenderer));

            newLayer.transform.SetParent(newGrid.transform, false);
            Tilemap layerTilemap = newLayer.GetComponent <Tilemap>();

            layerTilemap.tileAnchor = new Vector2(0.5f, 0.5f);
            if (layer.opacity < 1.0f)
            {
                layerTilemap.color = new Color(1.0f, 1.0f, 1.0f, layer.opacity);
            }

            if (layer.data.chunks != null)
            {
                for (int c = 0; c < layer.data.chunks.Length; c++)
                {
                    TMX.Chunk chunk   = layer.data.chunks[c];
                    bool      success = AddChunkToTilemap(layerTilemap, layer.data.encoding, layer.data.compression, chunk.tiles, chunk.text,
                                                          chunk.x, chunk.y, chunk.width, chunk.height, importedTilesets, cellWidth, cellHeight);
                    if (!success)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                bool success = AddChunkToTilemap(layerTilemap, layer.data.encoding, layer.data.compression, layer.data.tiles, layer.data.text,
                                                 0, 0, layer.width, layer.height, importedTilesets, cellWidth, cellHeight);
                if (!success)
                {
                    return(false);
                }
            }

            TilemapRenderer renderer = newLayer.GetComponent <TilemapRenderer>();

            renderer.sortingOrder = layerID;
            renderer.sortOrder    = tileSortOrder;
            if (!layer.visible)
            {
                renderer.enabled = false;
            }

            IDictionary <string, string> properties = (layer.properties == null ? new Dictionary <string, string>() : layer.properties.ToDictionary());

            foreach (ITilemapImportOperation operation in importOperations)
            {
                operation.HandleCustomProperties(newLayer, properties);
            }
            Undo.RegisterCreatedObjectUndo(newLayer, "Import tile layer");
            return(true);
        }
예제 #2
0
        /* Entry point into this class */
        public static bool ImportTMXFile(string path, string inTilesetDir, Grid targetGrid, string inImageLayerSpriteDir, bool setHiddenLayersToInactive)
        {
            s_tmxParentFolder = Path.GetDirectoryName(path);
            string filename = Path.GetFileNameWithoutExtension(path);

            s_imageLayerSpriteDir       = inImageLayerSpriteDir;
            s_tilesetDir                = inTilesetDir;
            s_orderInLayer              = 0;
            s_setHiddenLayersToInactive = setHiddenLayersToInactive;

            s_map = ImportUtils.ReadXMLIntoObject <TMX.Map>(path);
            if (s_map == null)
            {
                return(false);
            }
            if (s_map.backgroundcolor != null)
            {
                Color backgroundColor;
                if (ColorUtility.TryParseHtmlString(s_map.backgroundcolor, out backgroundColor))
                {
                    Camera.main.backgroundColor = backgroundColor;
                }
            }
            if (s_map.tilesets != null)
            {
                // First we need to load (or import) all the tilesets referenced by the TMX file...
                s_cellWidth        = s_map.tilewidth;
                s_cellHeight       = s_map.tileheight;
                s_pixelsPerUnit    = Mathf.Max(s_map.tilewidth, s_map.tileheight);
                s_importedTilesets = new ImportedTileset[s_map.tilesets.Length];
                for (int i = 0; i < s_map.tilesets.Length; i++)
                {
                    s_importedTilesets[i] = TiledTSXImporter.ImportFromTilesetReference(s_map.tilesets[i], s_tmxParentFolder, s_tilesetDir, s_cellWidth, s_cellHeight, s_pixelsPerUnit);
                    if (s_importedTilesets[i] == null || s_importedTilesets[i].tiles == null || s_importedTilesets[i].tiles[0] == null)
                    {
                        Debug.LogError("Imported tileset is incomplete");
                        return(false);
                    }
                }


                // Set up the Grid to store everything in
                s_gridGO = PrepareGrid(filename, targetGrid);

                s_importOperations = ImportUtils.GetObjectsThatImplementInterface <ITilemapImportOperation>();
                s_sortOrder        = TilemapRenderer.SortOrder.TopLeft;
                if (s_map.renderorder != null)
                {
                    if (s_map.renderorder.Equals("right-down"))
                    {
                        s_sortOrder = TilemapRenderer.SortOrder.TopLeft;
                    }
                    else if (s_map.renderorder.Equals("right-up"))
                    {
                        s_sortOrder = TilemapRenderer.SortOrder.BottomLeft;
                    }
                    else if (s_map.renderorder.Equals("left-down"))
                    {
                        s_sortOrder = TilemapRenderer.SortOrder.TopRight;
                    }
                    else if (s_map.renderorder.Equals("left-up"))
                    {
                        s_sortOrder = TilemapRenderer.SortOrder.BottomRight;
                    }
                }
                // Unity's isometric rendering only works well with TopRight sortorder
                if (s_map.orientation == "isometric")
                {
                    s_sortOrder = TilemapRenderer.SortOrder.TopRight;
                }

                // Unity hex grid only supports even stagger index, so we need a workaround to compensate
                TileLayerImporter.s_needsHexOddToEvenConversion = s_map.orientation == "hexagonal" && s_map.staggerindex == "odd";
                // Unity's isometric rendering is 90 degrees rotated anti-clockwise on the grid compared to Tiled,
                // so we need to rotate the grid clockwise 90 degrees to look correct in Unity
                TileLayerImporter.s_needsGridRotationToMatchUnityIsometric = s_map.orientation == "isometric";

                ObjectLayerImporter.s_importedTemplates = new Dictionary <string, ImportedTemplate>();

                bool loadedLayers = false;
                if (s_map.topLevelLayers != null)
                {
                    loadedLayers = TreatLayers(s_gridGO, s_map.topLevelLayers);
                }
                else
                {
                    loadedLayers = true;
                }

                // Handle the complete map's properties
                if (loadedLayers)
                {
                    HandleCustomProperties(s_gridGO, s_map.properties);
                }
            }

            return(true);
        }
예제 #3
0
        public static bool ImportTMXFile(string path, string tilesetDir, Grid targetGrid)
        {
            string tmxParentFolder = Path.GetDirectoryName(path);
            string filename        = Path.GetFileNameWithoutExtension(path);

            TMX.Map map = ImportUtils.ReadXMLIntoObject <TMX.Map>(path);
            if (map == null)
            {
                return(false);
            }
            if (map.backgroundcolor != null)
            {
                Color backgroundColor;
                if (ColorUtility.TryParseHtmlString(map.backgroundcolor, out backgroundColor))
                {
                    Camera.main.backgroundColor = backgroundColor;
                }
            }
            if (map.tilesets != null)
            {
                // First we need to load (or import) all the tilesets referenced by the TMX file...
                int cellWidth     = map.tilewidth;
                int cellHeight    = map.tileheight;
                int pixelsPerUnit = Mathf.Max(map.tilewidth, map.tileheight);
                ImportedTileset[] importedTilesets = new ImportedTileset[map.tilesets.Length];
                for (int i = 0; i < map.tilesets.Length; i++)
                {
                    importedTilesets[i] = TiledTSXImporter.ImportFromTilesetReference(map.tilesets[i], tmxParentFolder, tilesetDir, cellWidth, cellHeight, pixelsPerUnit);
                    if (importedTilesets[i] == null || importedTilesets[i].tiles == null || importedTilesets[i].tiles[0] == null)
                    {
                        Debug.LogError("Imported tileset is incomplete");
                        return(false);
                    }
                }


                // Set up the Grid to store everything in
                GameObject newGrid = null;
                if (targetGrid != null)
                {
                    newGrid = targetGrid.gameObject;
                    for (int i = newGrid.transform.childCount - 1; i >= 0; --i)
                    {
                        Undo.DestroyObjectImmediate(newGrid.transform.GetChild(i).gameObject);
                    }
                }
                else
                {
                    newGrid = new GameObject(filename, typeof(Grid));
                    newGrid.GetComponent <Grid>().cellSize = new Vector3(1.0f, 1.0f, 0.0f);
                    Undo.RegisterCreatedObjectUndo(newGrid, "Import map to new Grid");
                }

                ITilemapImportOperation[] importOperations = ImportUtils.GetObjectsThatImplementInterface <ITilemapImportOperation>();
                TilemapRenderer.SortOrder sortOrder        = TilemapRenderer.SortOrder.TopLeft;
                if (map.renderorder.Equals("right-down"))
                {
                    sortOrder = TilemapRenderer.SortOrder.TopLeft;
                }
                else if (map.renderorder.Equals("right-up"))
                {
                    sortOrder = TilemapRenderer.SortOrder.BottomLeft;
                }
                else if (map.renderorder.Equals("left-down"))
                {
                    sortOrder = TilemapRenderer.SortOrder.TopRight;
                }
                else if (map.renderorder.Equals("left-up"))
                {
                    sortOrder = TilemapRenderer.SortOrder.BottomRight;
                }
                // Load all the tile layers
                if (map.layers != null)
                {
                    for (int i = 0; i < map.layers.Length; i++)
                    {
                        TMX.Layer layer = map.layers[i];

                        bool success = ImportTileLayer(layer, newGrid, i, importedTilesets, importOperations, sortOrder, cellWidth, cellHeight, map.infinite);
                        if (!success)
                        {
                            return(false);
                        }
                    }
                }

                Dictionary <string, ImportedTemplate> importedTemplates = new Dictionary <string, ImportedTemplate>();

                // Load all the objects
                if (map.objectgroups != null)
                {
                    for (int g = 0; g < map.objectgroups.Length; g++)
                    {
                        TMX.ObjectGroup objectGroup = map.objectgroups[g];

                        if (objectGroup != null && objectGroup.objects != null && objectGroup.objects.Length > 0)
                        {
                            GameObject newObjectLayer = new GameObject(objectGroup.name);
                            newObjectLayer.transform.SetParent(newGrid.transform, false);
                            Undo.RegisterCreatedObjectUndo(newObjectLayer, "Import object layer");

                            int sortingLayer = (map.layers == null ? 0 : map.layers.Length) + g;

                            for (int i = 0; i < objectGroup.objects.Length; i++)
                            {
                                TMX.Object mapObject = objectGroup.objects[i];

                                bool success = ImportMapObject(mapObject, importedTilesets, importedTemplates, newObjectLayer, map.tilewidth, map.tileheight, sortingLayer, importOperations, tilesetDir, cellWidth, cellHeight, pixelsPerUnit, tmxParentFolder);
                                if (!success)
                                {
                                    return(false);
                                }
                            }
                            Color objectColour = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                            if (objectGroup.color != null)
                            {
                                ColorUtility.TryParseHtmlString(objectGroup.color, out objectColour);
                            }
                            if (objectGroup.opacity != 1.0f)
                            {
                                objectColour.a = objectGroup.opacity;
                            }
                            SpriteRenderer[] renderers = newObjectLayer.GetComponentsInChildren <SpriteRenderer>();
                            foreach (SpriteRenderer r in renderers)
                            {
                                r.color = objectColour;
                            }
                            if (!objectGroup.visible)
                            {
                                foreach (SpriteRenderer r in renderers)
                                {
                                    r.enabled = false;
                                }
                            }

                            IDictionary <string, string> properties = (objectGroup.properties == null ? new Dictionary <string, string>() : objectGroup.properties.ToDictionary());

                            foreach (ITilemapImportOperation operation in importOperations)
                            {
                                operation.HandleCustomProperties(newObjectLayer, properties);
                            }
                        }
                    }
                }
            }

            return(true);
        }