Пример #1
0
        static TMX.Object ApplyTemplate(TMX.Object mapObject, string tmxParentFolder, string tilesetDir, int cellWidth, int cellHeight, int pixelsPerUnit,
                                        Dictionary <string, ImportedTemplate> importedTemplates, out ImportedTileset replacementTileset)
        {
            // Template path seems to be relative to the TMX
            string templatePath = Path.GetFullPath(Path.Combine(tmxParentFolder, mapObject.template));

            ImportedTemplate importedTemplate;

            if (importedTemplates.ContainsKey(templatePath))
            {
                importedTemplate = importedTemplates[templatePath];
            }
            else
            {
                importedTemplate = TiledTXImporter.LoadTXFile(templatePath, tilesetDir, cellWidth, cellHeight, pixelsPerUnit);
                importedTemplates.Add(templatePath, importedTemplate);
            }

            if (!mapObject.gid.HasValue)
            {
                replacementTileset = importedTemplate.m_importedTileset;
            }
            else
            {
                replacementTileset = null; // If the instance has a gid, assume it uses the normal map tilesets
            }
            return(mapObject.GetVersionWithTemplateApplied(importedTemplate.m_template.templateObject));
        }
Пример #2
0
        internal static bool ImportObjectGroup(TMX.ObjectGroup objectGroup, ref int layerID, GameObject parent, out GameObject newObjectLayer)
        {
            newObjectLayer = null;
            if (objectGroup != null && objectGroup.objects != null && objectGroup.objects.Length > 0)
            {
                newObjectLayer = new GameObject(objectGroup.name);
                newObjectLayer.transform.SetParent(parent.transform, false);
                TiledTMXImporter.SetupLayerOffset(newObjectLayer, objectGroup.offsetx, objectGroup.offsety);

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

                    bool success = ImportMapObject(mapObject, TiledTMXImporter.s_importedTilesets, newObjectLayer, TiledTMXImporter.s_map.tilewidth, TiledTMXImporter.s_map.tileheight, layerID);
                    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;
                }
            }
            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);
        }
Пример #4
0
        static bool ImportMapObject(TMX.Object mapObject, ImportedTileset[] importedTilesets, Dictionary <string, ImportedTemplate> importedTemplates, GameObject newObjectLayer, int mapTileWidth, int mapTileHeight, int sortingLayer,
                                    ITilemapImportOperation[] importOperations, string tilesetDir, int cellWidth, int cellHeight, int pixelsPerUnit, string tmxParentFolder)
        {
            ImportedTileset replacementTileset = null;

            if (mapObject.template != null)
            {
                // Fill out empty object fields with data from the template.
                // The template could have a tile from it's own tileset reference, so in that case, use the template tileset instead, and the template object GID
                TMX.Object combinedMapObject = ApplyTemplate(mapObject, tmxParentFolder, tilesetDir, cellWidth, cellHeight, pixelsPerUnit, importedTemplates, out replacementTileset);

                if (combinedMapObject == null)
                {
                    Debug.LogError("Could not load template for map object " + mapObject);
                    return(false);
                }
                else
                {
                    mapObject = combinedMapObject;
                }
            }
            mapObject.InitialiseUnsetValues(); // We need special code to set defaults here, because setting them before merging with a template would give incorrect results.

            // Use the template's tileset (and the gid that's been set by ApplyTemplate)
            if (replacementTileset != null)
            {
                importedTilesets = new ImportedTileset[] { replacementTileset };
            }

            ImportedTile importedTile;

            TSX.Tile  tilesetTile; // Unused
            Matrix4x4 matrix;

            TiledUtils.FindTileDataAndMatrix(mapObject.gid.Value, importedTilesets, cellWidth, cellHeight, out importedTile, out tilesetTile, out matrix);

            Vector2 pixelsToUnits = new Vector2(1.0f / mapTileWidth, -1.0f / mapTileHeight);

            GameObject newObject = new GameObject(mapObject.name);

            newObject.transform.SetParent(newObjectLayer.transform, false);

            // So we gain the tile rotation/flipping
            newObject.transform.FromMatrix(matrix);

            Vector2 corner = Vector2.Scale(new Vector2(mapObject.x.Value, mapObject.y.Value), pixelsToUnits);

            if (importedTile != null)
            {
                Tile    unityTile          = importedTile.tile;
                Vector2 pivotProportion    = new Vector2(unityTile.sprite.pivot.x / unityTile.sprite.rect.width, unityTile.sprite.pivot.y / unityTile.sprite.rect.height);
                Vector3 pivotWorldPosition = corner + Vector2.Scale(new Vector2(mapObject.width.Value * pivotProportion.x, mapObject.height.Value * -pivotProportion.y), pixelsToUnits);
                newObject.transform.localPosition += pivotWorldPosition;

                SpriteRenderer renderer = newObject.AddComponent <SpriteRenderer>();
                renderer.sprite       = unityTile.sprite;
                renderer.sortingOrder = sortingLayer;
                if (unityTile.colliderType == Tile.ColliderType.Sprite)
                {
                    newObject.AddComponent <PolygonCollider2D>();
                }
                else if (unityTile.colliderType == Tile.ColliderType.Grid)
                {
                    newObject.AddComponent <BoxCollider2D>();
                }
                Vector2 scale = new Vector2(mapObject.width.Value / unityTile.sprite.rect.width, mapObject.height.Value / unityTile.sprite.rect.height);
                newObject.transform.localScale = Vector3.Scale(newObject.transform.localScale, new Vector3(scale.x, scale.y, 1.0f));
            }
            else
            {
                Vector3 pivotWorldPosition = corner + Vector2.Scale(new Vector2(mapObject.width.Value * 0.5f, mapObject.height.Value * 0.5f), pixelsToUnits);
                newObject.transform.localPosition += pivotWorldPosition;
                // If no tile used, must be a non-tile object of some sort (collision or text)
                if (mapObject.ellipse != null)
                {
                    EllipseCollider2D collider = newObject.AddComponent <EllipseCollider2D>();
                    collider.RadiusX = (mapObject.width.Value * 0.5f) / mapTileWidth;
                    collider.RadiusY = (mapObject.height.Value * 0.5f) / mapTileHeight;
                }
                else if (mapObject.polygon != null)
                {
                    PolygonCollider2D collider = newObject.AddComponent <PolygonCollider2D>();
                    string            points   = mapObject.polygon.points;
                    collider.points = ImportUtils.PointsFromString(points, pixelsToUnits);
                }
                else if (mapObject.polyline != null)
                {
                    EdgeCollider2D collider = newObject.AddComponent <EdgeCollider2D>();
                    string         points   = mapObject.polyline.points;
                    collider.points = ImportUtils.PointsFromString(points, pixelsToUnits);
                }
                else if (mapObject.text != null)
                {
                    TextMesh textMesh = newObject.AddComponent <TextMesh>();
                    textMesh.text   = mapObject.text.text;
                    textMesh.anchor = TextAnchor.MiddleCenter;

                    Color color = Color.white;
                    if (mapObject.text.color != null)
                    {
                        ColorUtility.TryParseHtmlString(mapObject.text.color, out color);
                    }
                    textMesh.color = color;

                    // Saving an OS font as an asset in unity (through script) is seemingly impossible right now in a platform independent way
                    // So we'll skip fonts for now

                    textMesh.fontSize = (int)mapObject.text.pixelsize; // Guess a good resolution for the font
                    float targetWorldTextHeight = (float)mapObject.text.pixelsize / (float)mapTileHeight;
                    textMesh.characterSize = targetWorldTextHeight * 10.0f / textMesh.fontSize;

                    Renderer renderer = textMesh.gameObject.GetComponent <MeshRenderer>();
                    renderer.sortingOrder   = sortingLayer;
                    renderer.sortingLayerID = SortingLayer.GetLayerValueFromName("Default");
                }
                else
                {
                    // Regular box collision
                    BoxCollider2D collider = newObject.AddComponent <BoxCollider2D>();
                    collider.size = new Vector2(mapObject.width.Value / mapTileWidth, mapObject.height.Value / mapTileHeight);
                }
            }

            if (mapObject.rotation != 0.0f)
            {
                newObject.transform.RotateAround(corner, new Vector3(0.0f, 0.0f, 1.0f), -mapObject.rotation.Value);
            }

            if (mapObject.visible == false)
            {
                Renderer renderer = newObject.GetComponent <Renderer>();
                if (renderer != null)
                {
                    renderer.enabled = false;
                }
            }

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

            foreach (ITilemapImportOperation operation in importOperations)
            {
                operation.HandleCustomProperties(newObject, properties);
            }
            return(true);
        }