Esempio n. 1
0
        public static TmxTilemap LoadFromFile(string sFilePath)
        {
            XMLSerializer objSerializer = new XMLSerializer();
            TmxMap        map           = objSerializer.LoadFromXMLFile <TmxMap>(sFilePath);

            map.FixExportedTilesets(Path.GetDirectoryName(sFilePath));

            TmxTilemap tilemap_ret = new TmxTilemap(map);

            tilemap_ret.FilePathDirectory = Path.GetDirectoryName(sFilePath);

            tilemap_ret.LoadImageData();

            return(tilemap_ret);
        }
Esempio n. 2
0
        public static void CreateTilesetFromTmx()
        {
            string path        = PlayerPrefs.GetString(k_keyLastOpenFilePanelPath);
            string tmxFilePath = EditorUtility.OpenFilePanel("Create Tileset From TMX File", path, "tmx");

            if (!string.IsNullOrEmpty(tmxFilePath))
            {
                PlayerPrefs.SetString(k_keyLastOpenFilePanelPath, Path.GetDirectoryName(tmxFilePath));
                TmxTilemap tilemap = TmxTilemap.LoadFromFile(tmxFilePath);
                if (tilemap.DicTilesetTex2D.Values.Count == 0)
                {
                    Debug.LogError("Not tileset found!");
                    return;
                }

                CreateTilesetFromTmx(tmxFilePath, SuperTilemapEditor.EditorUtils.GetAssetSelectedPath());
            }
        }
Esempio n. 3
0
        public static Tileset CreateTilesetFromTmx(string tmxFilePath, string dstPath)
        {
            string     tmxFileName = Path.GetFileNameWithoutExtension(tmxFilePath);
            TmxTilemap tilemap     = TmxTilemap.LoadFromFile(tmxFilePath);

            if (tilemap.DicTilesetTex2D.Values.Count == 0)
            {
                return(null);
            }

            //NOTE: calling this after PackTextures will make the atlasTexture to be null sometimes
            Tileset tilesetAsset     = ScriptableObject.CreateInstance <Tileset>();
            string  tilesetAssetPath = Path.Combine(dstPath, tmxFileName + "Tileset.asset");

            AssetDatabase.CreateAsset(tilesetAsset, tilesetAssetPath);
            Texture2D atlasTexture;

            Rect[] tilesetRects;
            if (tilemap.DicTilesetTex2D.Values.Count == 1)
            {
                atlasTexture = tilemap.DicTilesetTex2D.Values.ToArray()[0].tilesetTexture;
                tilesetRects = new Rect[] { new Rect(0, 0, atlasTexture.width, atlasTexture.height) };
            }
            else
            {
                atlasTexture = new Texture2D(8192, 8192, TextureFormat.ARGB32, false, false);
                tilesetRects = atlasTexture.PackTextures(tilemap.DicTilesetTex2D.Values.Select(x => x.tilesetTexture).ToArray(), 0, 8192);
            }
            string atlasPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(tilesetAsset)) + "/" + tmxFileName + "Atlas.png";

            atlasTexture.hideFlags = HideFlags.None;
            AssetDatabase.CreateAsset(atlasTexture, atlasPath);
            atlasTexture.hideFlags = HideFlags.DontSave;
            File.WriteAllBytes(atlasPath, atlasTexture.EncodeToPNG());
            ImportTexture(atlasPath);
            AssetDatabase.Refresh();

            //NOTE: tilesetAsset is sometimes nulled after calling Refresh
            tilesetAsset = AssetDatabase.LoadAssetAtPath <Tileset>(tilesetAssetPath);
            // Link Atlas with asset to be able to save it in the prefab
            tilesetAsset.AtlasTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Texture2D));

            tilesetAsset.TilePxSize = new Vector2(tilemap.Map.Tilesets[0].TileWidth, tilemap.Map.Tilesets[0].TileHeight);
            int         tilesetIdx = 0;
            List <Tile> tileList   = new List <Tile>();

            foreach (TmxTilemap.TilesetTextureData tilesetTextureData in tilemap.DicTilesetTex2D.Values)
            {
                TmxTileset tmxTileset = tilemap.Map.Tilesets[tilesetIdx];
                Vector2    tileSize   = new Vector2(tilemap.Map.Tilesets[tilesetIdx].TileWidth, tilemap.Map.Tilesets[tilesetIdx].TileHeight);
                Rect[]     tileRects  = tilesetTextureData.isCollectionOfSprites?
                                        tilesetTextureData.tileRects
                    :
                                        GenerateGridSpriteRectangles(tilesetTextureData.tilesetTexture, new Vector2(tmxTileset.Margin, tmxTileset.Margin), tileSize, new Vector2(tmxTileset.Spacing, tmxTileset.Spacing));
                TileSelection tilesetSelection = tilesetTextureData.isCollectionOfSprites?
                                                 new TileSelection(tilesetTextureData.tileIds.Select(i => (uint)i).ToList(), tmxTileset.Columns)
                    :
                                                 new TileSelection(Enumerable.Range(tileList.Count, tileRects.Length).Select(i => (uint)i).ToList(), tmxTileset.Columns);
                for (int i = 0; i < tileRects.Length; ++i)
                {
                    // fill with padding tiles the spaces between tiles. In tiled you can remove and add tiles to sprite collections tileset, so the ids could not be consecutive.
                    if (tilesetTextureData.isCollectionOfSprites)
                    {
                        int paddingTiles = i == 0? tilesetTextureData.tileIds[i] : (tilesetTextureData.tileIds[i] - tilesetTextureData.tileIds[i - 1]) - 1;
                        if (paddingTiles > 0)
                        {
                            tileList.AddRange(Enumerable.Repeat(default(Tile), paddingTiles));
                        }
                    }
                    Rect tileRect = tileRects[i];
                    Rect uv       = tileRect;
                    uv.xMin     /= tilesetAsset.AtlasTexture.width;
                    uv.xMax     /= tilesetAsset.AtlasTexture.width;
                    uv.yMin     /= tilesetAsset.AtlasTexture.height;
                    uv.yMax     /= tilesetAsset.AtlasTexture.height;
                    uv.position += tilesetRects[tilesetIdx].position;
                    tileList.Add(new Tile()
                    {
                        uv = uv
                    });
                }
                tilesetIdx++;
                tilesetAsset.TileViews.Add(new TileView(tilesetTextureData.tilesetTexture.name, tilesetSelection));
            }
            tilesetAsset.SetTiles(tileList);
            //NOTE: sometimes, the asset is not saved, this makes sure tilesetAsset is saved with the new data
            TilemapUtilsEditor.CreateOrReplaceAsset <Tileset>(tilesetAsset, tilesetAssetPath);
            return(tilesetAsset);
        }
Esempio n. 4
0
        public static void ImportTmxIntoTheScene(string tmxFilePath, Tileset tileset)
        {
            string     tmxFileName = Path.GetFileNameWithoutExtension(tmxFilePath);
            TmxTilemap tilemap     = TmxTilemap.LoadFromFile(tmxFilePath);

            if (tilemap.DicTilesetTex2D.Values.Count == 0)
            {
                return;
            }

            GameObject   tilemapGroupObj = new GameObject(tmxFileName);
            TilemapGroup tilemapGroup    = tilemapGroupObj.AddComponent <TilemapGroup>();
            int          orderInLayer    = 0;

            foreach (TmxLayer layer in tilemap.Map.Layers)
            {
                GameObject tilemapObj = new GameObject(layer.Name);
                tilemapObj.transform.SetParent(tilemapGroupObj.transform);
                STETilemap tilemapBhv = tilemapObj.AddComponent <STETilemap>();
                tilemapBhv.Tileset      = tileset;
                tilemapBhv.OrderInLayer = orderInLayer++;
                tilemapBhv.IsVisible    = layer.Visible;
                tilemapBhv.TintColor    = new Color(1f, 1f, 1f, layer.Opacity);
                for (int gx = 0; gx < layer.Width; gx++)
                {
                    for (int gy = 0; gy < layer.Height; gy++)
                    {
                        int          tileIdx = gy * layer.Width + gx;
                        TmxLayerTile tile    = layer.Tiles[tileIdx];

                        //skip non valid tiles
                        if (tile.GId == 0)
                        {
                            continue;
                        }

                        int  tileId   = tilemap.GetTileAbsoluteId(tile);
                        uint tileData = tileId >= 0 ? (uint)tileId : Tileset.k_TileData_Empty;
                        if (tileData != Tileset.k_TileData_Empty)
                        {
                            // add tile flags
                            if ((tile.GId & k_FLIPPED_HORIZONTALLY_FLAG) != 0)
                            {
                                tileData |= Tileset.k_TileFlag_FlipH;
                            }
                            if ((tile.GId & k_FLIPPED_VERTICALLY_FLAG) != 0)
                            {
                                tileData |= Tileset.k_TileFlag_FlipV;
                            }
                            if ((tile.GId & k_FLIPPED_DIAGONALLY_FLAG) != 0)
                            {
                                tileData |= Tileset.k_TileFlag_Rot90;
                            }
                            //convert from tiled flip diagonal to rot90
                            if ((tileData & Tileset.k_TileFlag_Rot90) != 0)
                            {
                                if (((tile.GId & k_FLIPPED_HORIZONTALLY_FLAG) != 0) != ((tile.GId & k_FLIPPED_VERTICALLY_FLAG) != 0))
                                {
                                    tileData ^= Tileset.k_TileFlag_FlipH;
                                }
                                else
                                {
                                    tileData ^= Tileset.k_TileFlag_FlipV;
                                }
                            }
                        }
                        tilemapBhv.SetTileData(gx, layer.Height - gy - 1, tileData);
                    }
                }
                tilemapBhv.UpdateMesh();
            }
            tilemapGroup.Refresh();
        }
Esempio n. 5
0
        public static Tileset CreateTilesetFromTmx(string tmxFilePath, string dstPath)
        {
            string     tmxFileName = Path.GetFileNameWithoutExtension(tmxFilePath);
            TmxTilemap tilemap     = TmxTilemap.LoadFromFile(tmxFilePath);

            if (tilemap.DicTilesetTex2D.Values.Count == 0)
            {
                return(null);
            }

            //NOTE: calling this after PackTextures will make the atlasTexture to be null sometimes
            Tileset tilesetAsset = ScriptableObject.CreateInstance <Tileset>();

            AssetDatabase.CreateAsset(tilesetAsset, Path.Combine(dstPath, tmxFileName + "Tileset.asset"));
            Texture2D atlasTexture;

            Rect[] tilesetRects;
            if (tilemap.DicTilesetTex2D.Values.Count == 1)
            {
                atlasTexture = tilemap.DicTilesetTex2D.Values.ToArray()[0];
                tilesetRects = new Rect[] { new Rect(0, 0, atlasTexture.width, atlasTexture.height) };
            }
            else
            {
                atlasTexture = new Texture2D(8192, 8192, TextureFormat.ARGB32, false, false);
                tilesetRects = atlasTexture.PackTextures(tilemap.DicTilesetTex2D.Values.ToArray(), 0);
            }
            string atlasPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(tilesetAsset)) + "/" + tmxFileName + "Atlas.png";

            AssetDatabase.CreateAsset(atlasTexture, atlasPath);
            File.WriteAllBytes(atlasPath, atlasTexture.EncodeToPNG());
            ImportTexture(atlasPath);
            AssetDatabase.Refresh();

            // Link Atlas with asset to be able to save it in the prefab
            tilesetAsset.AtlasTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Texture2D));

            tilesetAsset.TilePxSize = new Vector2(tilemap.Map.Tilesets[0].TileWidth, tilemap.Map.Tilesets[0].TileHeight);
            int         tilesetIdx = 0;
            List <Tile> tileList   = new List <Tile>();

            foreach (Texture2D tilesetTexture in tilemap.DicTilesetTex2D.Values)
            {
                TmxTileset    tmxTileset       = tilemap.Map.Tilesets[tilesetIdx];
                Vector2       tileSize         = new Vector2(tilemap.Map.Tilesets[0].TileWidth, tilemap.Map.Tilesets[0].TileHeight);
                Rect[]        tileRects        = GenerateGridSpriteRectangles(tilesetTexture, new Vector2(tmxTileset.Margin, tmxTileset.Margin), tileSize, new Vector2(tmxTileset.Spacing, tmxTileset.Spacing));
                TileSelection tilesetSelection = new TileSelection(Enumerable.Range(tileList.Count, tileRects.Length).Select(i => (uint)i).ToList(), tmxTileset.Columns);
                foreach (Rect tileRect in tileRects)
                {
                    Rect uv = tileRect;
                    uv.xMin     /= tilesetAsset.AtlasTexture.width;
                    uv.xMax     /= tilesetAsset.AtlasTexture.width;
                    uv.yMin     /= tilesetAsset.AtlasTexture.height;
                    uv.yMax     /= tilesetAsset.AtlasTexture.height;
                    uv.position += tilesetRects[tilesetIdx].position;
                    tileList.Add(new Tile()
                    {
                        uv = uv
                    });
                }
                tilesetIdx++;
                tilesetAsset.TileViews.Add(new TileView(tilesetTexture.name, tilesetSelection));
            }
            tilesetAsset.SetTiles(tileList);
            return(tilesetAsset);
        }