Example #1
0
        static bool CreateTileAssets(TSX.Tile[] tiles, bool singleImageTileset, string tilesetName, int pixelsPerUnit, Sprite[] tileSprites, string[] tileTargetPaths, out ImportedTile[] outputTiles)
        {
            AssetDatabase.StartAssetEditing();
            Debug.Assert(tileSprites != null && tileTargetPaths != null);
            outputTiles = new ImportedTile[tileTargetPaths.Length];
            if (tileSprites.Length != tileTargetPaths.Length)
            {
                Debug.Log("We have " + tileSprites.Length + " sprites but trying to create " + tileTargetPaths.Length + " tiles");
                AssetDatabase.StopAssetEditing();
                return(false);
            }

            bool success = true;

            for (int i = 0; i < tileTargetPaths.Length; i++)
            {
                EditorUtility.DisplayProgressBar("Importing " + tilesetName + " tileset tiles", null, (float)i / (float)tileTargetPaths.Length);
                if (tileSprites[i] != null)
                {
                    Tile.ColliderType colliderType = Tile.ColliderType.None;

                    // Tile Collision
                    if (!singleImageTileset && tiles != null)
                    {
                        TSX.Tile tile = tiles[i];
                        if (tile.HasCollisionData())
                        {
                            colliderType = Tile.ColliderType.Sprite;
                        }
                    }

                    // Tile Animation
                    TSX.Tile animationTile = null;
                    if (tiles != null)
                    {
                        if (singleImageTileset)
                        {
                            // Have to find relevant tile
                            foreach (TSX.Tile tile in tiles)
                            {
                                if (tile.id == i && tile.animation != null)
                                {
                                    animationTile = tile;
                                    break;
                                }
                            }
                        }
                        else if (tiles[i].animation != null)
                        {
                            animationTile = tiles[i];
                        }
                    }

                    if (animationTile != null)
                    {
                        TSX.Frame[] frames           = animationTile.animation.frames;
                        Sprite[]    animationSprites = new Sprite[frames.Length];
                        for (int f = 0; f < frames.Length; f++)
                        {
                            animationSprites[f] = tileSprites[frames[f].tileid];
                        }
                        //Just have to assume a constant animation speed ATM due to how Unity's Tilemap animation works :(
                        float        animationSpeed = 1.0f / (frames[0].duration / 1000.0f);
                        ImportedTile newTile        = CreateAnimatedTileAsset(tileSprites[i], animationSprites, animationSpeed, tileTargetPaths[i], colliderType);
                        success = newTile != null;
                        if (!success)
                        {
                            break;
                        }
                        outputTiles[i] = newTile;
                    }
                    else
                    {
                        ImportedTile newTile = CreateTileAsset(tileSprites[i], tileTargetPaths[i], colliderType);
                        success = newTile != null;
                        if (!success)
                        {
                            break;
                        }
                        outputTiles[i] = newTile;
                    }
                }
                else
                {
                    Debug.LogError("Sprite for tile " + tileTargetPaths[i] + " is null when creating Tile");
                }
            }
            AssetDatabase.StopAssetEditing();
            return(success);
        }
Example #2
0
        public static void FindTileDataAndMatrix(uint gid, ImportedTileset[] importedTilesets, int cellWidth, int cellHeight,
                                                 out ImportedTile importedTile, out TSX.Tile tilesetTile, out Matrix4x4 matrix)
        {
            importedTile = null;
            tilesetTile  = null;
            matrix       = Matrix4x4.identity;

            bool flippedHorizontally = (gid & FLIPPED_HORIZONTALLY_FLAG) != 0;
            bool flippedVertically   = (gid & FLIPPED_VERTICALLY_FLAG) != 0;
            bool flippedDiagonally   = (gid & FLIPPED_DIAGONALLY_FLAG) != 0;

            gid &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG);

            ImportedTileset tilesetContainingID = null;

            for (int j = importedTilesets.Length - 1; j >= 0; --j)
            {
                int firstGID = importedTilesets[j].firstGID;
                if (firstGID <= gid)
                {
                    tilesetContainingID = importedTilesets[j];
                    break;
                }
            }

            if (tilesetContainingID != null)
            {
                int relativeID = (int)gid - tilesetContainingID.firstGID;

                if (tilesetContainingID.tileset.IsSingleImageTileset())
                {
                    // A single-image-tileset will just order tiles from 0-n
                    importedTile = tilesetContainingID.tiles[relativeID];
                    tilesetTile  = null;
                }
                else
                {
                    for (int t = 0; t < tilesetContainingID.tileset.tiles.Length; t++)
                    {
                        TSX.Tile tile = tilesetContainingID.tileset.tiles[t];
                        int      id   = tile.id;
                        if (id == relativeID)
                        {
                            importedTile = tilesetContainingID.tiles[t];
                            tilesetTile  = tile;
                            break;
                        }
                    }
                }

                if (importedTile != null)
                {
                    if (importedTile.tile == null) // Load the tile asset if we haven't already
                    {
                        importedTile.tile = AssetDatabase.LoadAssetAtPath <Tile>(importedTile.path);
                    }
                    if (flippedHorizontally || flippedVertically || flippedDiagonally)
                    {
                        if (flippedDiagonally)
                        {
                            matrix = Matrix4x4.Rotate(Quaternion.Euler(0.0f, 0.0f, 90.0f));
                            matrix = Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f)) * matrix;
                        }

                        if (flippedHorizontally)
                        {
                            matrix = Matrix4x4.Scale(new Vector3(-1.0f, 1.0f, 1.0f)) * matrix;
                        }
                        if (flippedVertically)
                        {
                            matrix = Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f)) * matrix;
                        }

                        Rect rect = importedTile.tile.sprite.rect;
                        rect.x = -cellWidth * 0.5f;
                        rect.y = -cellHeight * 0.5f;

                        Vector2[] corners = new Vector2[4] {
                            new Vector2(rect.x, rect.y),
                            new Vector2(rect.x + rect.width, rect.y),
                            new Vector2(rect.x, rect.y + rect.height),
                            new Vector2(rect.x + rect.width, rect.y + rect.height)
                        };

                        for (int i = 0; i < corners.Length; i++)
                        {
                            corners[i] = matrix * corners[i];
                        }
                        Vector2 bottomLeftCorner = corners[0];
                        for (int i = 1; i < corners.Length; i++)
                        {
                            if (corners[i].x < bottomLeftCorner.x)
                            {
                                bottomLeftCorner.x = corners[i].x;
                            }
                            if (corners[i].y < bottomLeftCorner.y)
                            {
                                bottomLeftCorner.y = corners[i].y;
                            }
                        }
                        Vector2 offsetNeededUnits = new Vector2(-0.5f, -0.5f) - new Vector2(bottomLeftCorner.x / (float)cellWidth, bottomLeftCorner.y / (float)cellHeight);
                        matrix = Matrix4x4.Translate(offsetNeededUnits) * matrix;
                    }
                }
            }
        }