Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        static bool GenerateLayerObject(GameObject parent, TMX.BaseLayerElement layer, out GameObject layerObject)
        {
            layerObject = null;
            if (layer is TMX.Layer)
            {
                TMX.Layer tmxLayer = layer as TMX.Layer;

                bool success = TileLayerImporter.ImportTileLayer(tmxLayer, parent, s_orderInLayer, s_map.infinite, out layerObject);
                s_orderInLayer++;
                if (!success)
                {
                    return(false);
                }
            }
            else if (layer is TMX.ObjectGroup)
            {
                TMX.ObjectGroup objectGroup = layer as TMX.ObjectGroup;

                bool success = ObjectLayerImporter.ImportObjectGroup(objectGroup, ref s_orderInLayer, parent, out layerObject);
                s_orderInLayer++;
                if (!success)
                {
                    return(false);
                }
            }
            else if (layer is TMX.ImageLayer)
            {
                if (!ImportUtils.CreateAssetFolderIfMissing(s_imageLayerSpriteDir, true))
                {
                    return(false);
                }
                TMX.ImageLayer imageLayer = layer as TMX.ImageLayer;

                bool success = ImageLayerImporter.ImportImageLayer(imageLayer, parent, s_orderInLayer, out layerObject);
                s_orderInLayer++;
                if (!success)
                {
                    return(false);
                }
            }
            else if (layer is TMX.GroupLayer)
            {
                TMX.GroupLayer groupLayer = layer as TMX.GroupLayer;

                GameObject newGroupLayer = new GameObject(groupLayer.name);
                newGroupLayer.transform.SetParent(parent.transform, false);
                layerObject = newGroupLayer;
                SetupLayerOffset(newGroupLayer, groupLayer.offsetx, groupLayer.offsety);

                if (groupLayer.childLayers != null)
                {
                    TreatLayers(newGroupLayer, groupLayer.childLayers);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        internal static bool ImportTileLayer(TMX.Layer layer, GameObject parent, int layerID, bool infinite, out GameObject newLayer)
        {
            newLayer = null;
            if (infinite != (layer.data.chunks != null))
            {
                Debug.LogError("Our map infinite setting is " + infinite + " but our chunks value is " + layer.data.chunks);
                return(false);
            }
            newLayer = new GameObject(layer.name, typeof(Tilemap), typeof(TilemapRenderer));
            newLayer.transform.SetParent(parent.transform, false);
            TiledTMXImporter.SetupLayerOffset(newLayer, layer.offsetx, layer.offsety);
            Tilemap layerTilemap = newLayer.GetComponent <Tilemap>();
            Grid    tileGrid     = TiledTMXImporter.s_gridGO.GetComponent <Grid>();

            if (tileGrid.cellLayout == GridLayout.CellLayout.Hexagon)
            {
                layerTilemap.tileAnchor = new Vector3(0.0f, 0.0f); // Seems to play better with how Unity renders the hex sprites
            }
            else if (tileGrid.cellLayout == GridLayout.CellLayout.Isometric || tileGrid.cellLayout == GridLayout.CellLayout.IsometricZAsY)
            {
                layerTilemap.tileAnchor = new Vector3(0.0f, -1.0f); // Isometric tile anchors are at the bottom of the tile
            }
            else
            {
                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);
            }

            int     gridXOffset    = 0;
            int     gridYOffset    = 0;
            Vector3 offsetPosition = newLayer.transform.position;

            if (tileGrid.cellLayout == GridLayout.CellLayout.Hexagon)
            {
                HandleHexagonOffsetting(tileGrid.cellSize, out gridXOffset, out gridYOffset, out offsetPosition);
            }
            newLayer.transform.position = offsetPosition;
            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 + gridXOffset, chunk.y + gridYOffset, chunk.width, chunk.height);
                    if (!success)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                bool success = AddChunkToTilemap(layerTilemap, layer.data.encoding, layer.data.compression, layer.data.tiles, layer.data.text,
                                                 gridXOffset, gridYOffset, layer.width, layer.height);
                if (!success)
                {
                    return(false);
                }
            }

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

            renderer.sortingOrder = layerID;
            renderer.sortOrder    = TiledTMXImporter.s_sortOrder;

            return(true);
        }
Ejemplo n.º 4
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);
        }