Esempio n. 1
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. 2
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[tilesetIdx].TileWidth, tilemap.Map.Tilesets[tilesetIdx].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);
        }