コード例 #1
0
        static bool FillTilemapFromData(Tilemap tilemap, int startX, int startY, int width, int height, uint[] data)
        {
            bool anyTilesWithCollision = false;

            for (int i = 0; i < data.Length; i++)
            {
                uint value = data[i];

                ImportedTile importedTile;
                TSX.Tile     tilesetTile;
                Matrix4x4    matrix;
                TiledUtils.FindTileDataAndMatrix(value, TiledTMXImporter.s_importedTilesets, TiledTMXImporter.s_cellWidth, TiledTMXImporter.s_cellHeight, out importedTile, out tilesetTile, out matrix);

                if (importedTile != null && importedTile.tile != null)
                {
                    int x = startX + (i % width);
                    int y = -(startY + ((i / width) + 1));

                    Vector3Int pos = new Vector3Int(x, y, 0);
                    if (s_needsGridRotationToMatchUnityIsometric)
                    {
                        // Rotate 2D grid coordinates 90 degrees clockwise
                        pos.x = y;
                        pos.y = -x;
                    }

                    tilemap.SetTile(pos, importedTile.tile);
                    tilemap.SetTransformMatrix(pos, matrix);

                    if (importedTile.tile.colliderType != Tile.ColliderType.None)
                    {
                        anyTilesWithCollision = true;
                    }
                }
                else if (value > 0)
                {
                    Debug.LogError("Could not find tile " + value + " in tilemap " + tilemap.name);
                    if (ImportUtils.s_validationMode)
                    {
                        return(false);
                    }
                }
            }

            if (anyTilesWithCollision)
            {
                if (tilemap.gameObject.GetComponent <TilemapCollider2D>() == null)
                {
                    tilemap.gameObject.AddComponent <TilemapCollider2D>();
                }
            }

            return(true);
        }
コード例 #2
0
        static bool AddChunkToTilemap(Tilemap layerTilemap, string encoding, string compression, TMX.Tile[] plainTiles, string dataText,
                                      int x, int y, int width, int height)
        {
            uint[] gIDData = null;
            if (encoding == null)
            {
                TiledUtils.LoadDataFromPlainTiles(plainTiles, width, height, out gIDData);
            }
            else if (encoding.Equals("csv"))
            {
                if (!TiledUtils.LoadDataFromCSV(dataText, width, height, out gIDData))
                {
                    Debug.LogError("Layer data for layer " + layerTilemap.gameObject.name + " could not be csv decoded");
                    return(false);
                }
            }
            else if (encoding.Equals("base64"))
            {
                byte[] decoded = Convert.FromBase64String(dataText);
                if (decoded == null)
                {
                    Debug.LogError("Layer data for layer " + layerTilemap.gameObject.name + " could not be base64 decoded");
                    return(false);
                }
                if (compression != null)
                {
                    if (compression.Equals("zlib"))
                    {
                        decoded = ImportUtils.DecompressZLib(decoded);
                    }
                    else if (compression.Equals("gzip"))
                    {
                        decoded = ImportUtils.DecompressGZip(decoded);
                    }
                }
                if (!TiledUtils.LoadDataFromBytes(decoded, width, height, out gIDData))
                {
                    Debug.LogError("Layer data for layer " + layerTilemap.gameObject.name + " could not created from loaded byte data");
                    return(false);
                }
            }

            if (gIDData == null)
            {
                Debug.LogError("Layer data for layer " + layerTilemap.gameObject.name + " could not be decoded");
                return(false);
            }

            bool worked = FillTilemapFromData(layerTilemap, x, y, width, height, gIDData);

            return(worked);
        }
コード例 #3
0
 internal static bool ImportImageLayer(TMX.ImageLayer imageLayer, GameObject parent, int sortingLayer, out GameObject newImageLayer)
 {
     newImageLayer = new GameObject(imageLayer.name, typeof(SpriteRenderer));
     newImageLayer.transform.SetParent(parent.transform, false);
     TiledTMXImporter.SetupLayerOffset(newImageLayer, imageLayer.offsetx, imageLayer.offsety);
     if (imageLayer.image != null)
     {
         string         relativeSource = imageLayer.image.source;
         Sprite         importedSprite = TiledUtils.ImportPathAsSprite(TiledTMXImporter.s_tmxParentFolder, relativeSource, TiledTMXImporter.s_imageLayerSpriteDir, TiledTMXImporter.s_pixelsPerUnit);
         SpriteRenderer renderer       = newImageLayer.GetComponent <SpriteRenderer>();
         renderer.sprite       = importedSprite;
         renderer.sortingOrder = sortingLayer;
     }
     return(true);
 }
コード例 #4
0
        static bool FillTilemapFromData(Tilemap tilemap, int startX, int startY, int width, int height, uint[] data, ImportedTileset[] importedTilesets, int cellWidth, int cellHeight)
        {
            bool anyTilesWithCollision = false;

            for (int i = 0; i < data.Length; i++)
            {
                uint value = data[i];

                ImportedTile importedTile;
                TSX.Tile     tilesetTile;
                Matrix4x4    matrix;
                TiledUtils.FindTileDataAndMatrix(value, importedTilesets, cellWidth, cellHeight, out importedTile, out tilesetTile, out matrix);

                if (importedTile != null && importedTile.tile != null)
                {
                    int x = startX + (i % width);
                    int y = -(startY + ((i / width) + 1));

                    Vector3Int pos = new Vector3Int(x, y, 0);

                    tilemap.SetTile(pos, importedTile.tile);
                    tilemap.SetTransformMatrix(pos, matrix);

                    if (tilesetTile != null && tilesetTile.HasCollisionData())
                    {
                        anyTilesWithCollision = true;
                    }
                }
                else if (value > 0)
                {
                    Debug.LogError("Could not find tile " + value + " in tilemap " + tilemap.name);
                    if (ImportUtils.s_validationMode)
                    {
                        return(false);
                    }
                }
            }

            if (anyTilesWithCollision)
            {
                if (tilemap.gameObject.GetComponent <TilemapCollider2D>() == null)
                {
                    tilemap.gameObject.AddComponent <TilemapCollider2D>();
                }
            }

            return(true);
        }
コード例 #5
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);
        }