예제 #1
0
        public int GetTileAbsoluteId(TmxLayerTile tile)
        {
            // Value 0 means no tileset associated
            if (tile.GId == 0)
            {
                return(-1);
            }

            int tileGlobalId = (int)(tile.GId & 0x1FFFFFFF); // remove flip flags

            int tileAbsoluteId = 0;

            for (int tilesetIdx = 0; tilesetIdx < m_map.Tilesets.Count; tilesetIdx++)
            {
                TmxTileset tileset = m_map.Tilesets[tilesetIdx];
                if (tileGlobalId >= (tileset.FirstGId + tileset.TileCount))
                {
                    tileAbsoluteId += tileset.TileCount;
                }
                else
                {
                    tileAbsoluteId += (tileGlobalId - tileset.FirstGId);
                    break;
                }
            }

            return(tileAbsoluteId);
        }
예제 #2
0
        public TmxTileset FindTileset(TmxLayerTile tile)
        {
            // Value 0 means no tileset associated
            if (tile.GId == 0)
            {
                return(null);
            }

            int tilesetIdx;

            for (tilesetIdx = 0; tilesetIdx < m_map.Tilesets.Count; tilesetIdx++)
            {
                TmxTileset tileset = m_map.Tilesets[tilesetIdx];
                if (tileset.FirstGId > tile.GId)
                {
                    break;
                }
            }
            tilesetIdx--;

            return(m_map.Tilesets[tilesetIdx]);
        }
예제 #3
0
        void ImportIntoScene(TmxLayer layer)
        {
            GameObject layerObj = new GameObject("L:" + layer.Name);

            for (int tile_x = 0; tile_x < layer.Width; tile_x++)
            {
                for (int tile_y = 0; tile_y < layer.Height; tile_y++)
                {
                    int          tileIdx = tile_y * layer.Width + tile_x;
                    TmxLayerTile tile    = layer.Tiles[tileIdx];

                    //skip non valid tiles
                    if (tile.GId == 0)
                    {
                        continue;
                    }
                    int tileGlobalId = (int)(tile.GId & 0x1FFFFFFF); // remove flip flags

                    TmxTileset objTileset = FindTileset(tile);

                    // Draw Tile
                    Texture2D texTileset = m_dicTilesetTex2d[objTileset.FirstGId];
                    Rect      dstRect    = new Rect(tile_x * objTileset.TileWidth, (layer.Height - 1 - tile_y) * objTileset.TileHeight, objTileset.TileWidth, objTileset.TileHeight);

                    int  tileBaseIdx = tileGlobalId - objTileset.FirstGId;
                    int  scanLine    = objTileset.Image.Width / objTileset.TileWidth;
                    int  tileset_x   = tileBaseIdx % scanLine;
                    int  tileset_y   = tileBaseIdx / scanLine;
                    Rect srcRect     = new Rect(tileset_x * objTileset.TileWidth, texTileset.height - (tileset_y + 1) * objTileset.TileHeight, objTileset.TileWidth, objTileset.TileHeight);

                    GameObject tileObj = new GameObject("tile" + tile_x + "_" + tile_y);
                    tileObj.transform.SetParent(layerObj.transform);
                    SpriteRenderer tileRenderer = tileObj.AddComponent <SpriteRenderer>();
                    tileRenderer.sortingOrder       = s_sortingOrder;
                    tileRenderer.sprite             = Sprite.Create(texTileset, srcRect, Vector2.zero);
                    tileObj.transform.localPosition = dstRect.position / 100f;
                }
            }
        }
예제 #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();
        }