private Tilemap GetOrAddTilemapComponent(GameObject go, SuperTileLayer layer)
        {
            // If we already have a Tilemap component up our ancesters then we are using a format that does not support separate tilemaps
            var parentTilemap = go.GetComponentInParent <Tilemap>();

            if (parentTilemap == null)
            {
                // Need tilemap data if we're going to have tilemap for flips and rotations
                go.AddComponent <TilemapData>();

                var tilemap = go.AddComponent <Tilemap>();
                tilemap.tileAnchor         = m_MapComponent.GetTileAnchor();
                tilemap.animationFrameRate = SuperImportContext.Settings.AnimationFramerate;

                // Create the renderer for the layer
                var renderer = AddTilemapRendererComponent(go);
                AssignMaterial(renderer);

                if (layer != null)
                {
                    tilemap.color = new Color(1, 1, 1, layer.CalculateOpacity());
                    AssignSortingLayer(renderer, layer.m_SortingLayerName, layer.m_SortingOrder);
                }

                return(tilemap);
            }

            return(parentTilemap);
        }
        private void ProcessLayerDataChunk(GameObject goTilemap, Chunk chunk)
        {
            // Instantiate object to build all colliders for this chunk
            m_CurrentCollisionBuilder = new CollisionBuilder(goTilemap, m_TilePolygonDatabase, SuperImportContext);

            List <uint> tileIds = ReadTileIdsFromChunk(chunk);

            // preserve tile ids of layer
            TileIdsChunk tileIdsChunk = new TileIdsChunk()
            {
                _x       = chunk.X,
                _y       = chunk.Y,
                _width   = chunk.Width,
                _height  = chunk.Height,
                _tileIds = tileIds
            };
            SuperTileLayer superTileLayer = goTilemap.GetComponentInParent <SuperTileLayer>();

            superTileLayer.InternalAddTileIdsChunk(tileIdsChunk);

            PlaceTiles(goTilemap, chunk, tileIds);

            m_CurrentCollisionBuilder.Build(this);
            m_CurrentCollisionBuilder = null;
        }
Example #3
0
        private void ProcessLayerData(GameObject goLayer, XElement xData)
        {
            Assert.IsNotNull(goLayer);
            Assert.IsNotNull(xData);
            Assert.IsNull(m_CurrentTileLayer);

            m_CurrentTileLayer = goLayer.GetComponent <SuperTileLayer>();
            Assert.IsNotNull(m_CurrentTileLayer);

            // Create the tilemap for the layer if needed
            if (!m_TilesAsObjects && SuperImportContext.LayerIgnoreMode != LayerIgnoreMode.Visual)
            {
                GetOrAddTilemapComponent(goLayer);
            }
            else if (m_TilesAsObjects)
            {
                goLayer.AddComponent <SuperTilesAsObjectsTilemap>();
            }

            var chunk = new Chunk();

            chunk.Encoding    = xData.GetAttributeAs <DataEncoding>("encoding");
            chunk.Compression = xData.GetAttributeAs <DataCompression>("compression");

            // Are we reading in data in smaller chunks (for infinite maps) or one big chunk (the full map)
            var xChunks = xData.Elements("chunk");

            if (xChunks.Any())
            {
                foreach (var xChunk in xChunks)
                {
                    chunk.XmlChunk = xChunk;
                    chunk.X        = xChunk.GetAttributeAs <int>("x");
                    chunk.Y        = xChunk.GetAttributeAs <int>("y");
                    chunk.Width    = xChunk.GetAttributeAs <int>("width");
                    chunk.Height   = xChunk.GetAttributeAs <int>("height");

                    // Each chunk is a separate game object on the super layer
                    GameObject goChunk = new GameObject(string.Format("Chunk ({0},{1})", chunk.X, chunk.Y));
                    goLayer.AddChildWithUniqueName(goChunk);

                    ProcessLayerDataChunk(goChunk, chunk);
                }
            }
            else
            {
                // For regular maps the 'chunk' is the same as the layer data
                chunk.XmlChunk = xData;
                chunk.X        = 0;
                chunk.Y        = 0;
                chunk.Width    = m_MapComponent.m_Width;
                chunk.Height   = m_MapComponent.m_Height;

                ProcessLayerDataChunk(goLayer, chunk);
            }

            m_CurrentTileLayer = null;
        }
 public void EndTileLayer(SuperTileLayer layer)
 {
     if (IsInGroup())
     {
         CurrentTileZ++;
     }
     else
     {
         // Next tilemap will render on top of the this one
         m_CurrentSortOrder++;
     }
 }
Example #5
0
    /// <summary>
    /// Called by Unity on the frame when a script is enabled just before any of the Update methods are called the first time.
    /// </summary>
    private void Start()
    {
        SuperTileLayer[] superTileLayers = FindObjectsOfType <SuperTileLayer>();
        SuperTileLayer   testTileIdLayer = superTileLayers.FirstOrDefault(stl => stl.gameObject.name == "TestTileId");
        int tileId = testTileIdLayer.GetTileId(0, 0);

        Debug.Log(tileId);
        SuperTile customTile = testTileIdLayer.SuperMap.GetCustomTile(tileId);
        string    propVal    = customTile.GetPropertyValueAsString("testProp");

        Debug.Log(propVal);
    }
Example #6
0
        private void ProcessLayerData(GameObject goLayer, XElement xData)
        {
            Assert.IsNotNull(goLayer);
            Assert.IsNotNull(goLayer.GetComponent <SuperTileLayer>());
            Assert.IsNotNull(xData);

            SuperTileLayer superComp = goLayer.GetComponent <SuperTileLayer>();

            var chunk = new Chunk();

            chunk.Encoding    = xData.GetAttributeAs <DataEncoding>("encoding");
            chunk.Compression = xData.GetAttributeAs <DataCompression>("compression");

            bool tilesAsObjects = m_MapComponent.m_Orientation == MapOrientation.Isometric || m_TilesAsObjects;

            // Are we reading in data in smaller chunks (for infinite maps) or one big chunk (the full map)
            var xChunks = xData.Elements("chunk");

            if (xChunks.Any())
            {
                foreach (var xChunk in xChunks)
                {
                    chunk.XmlChunk = xChunk;
                    chunk.X        = xChunk.GetAttributeAs <int>("x");
                    chunk.Y        = xChunk.GetAttributeAs <int>("y");
                    chunk.Width    = xChunk.GetAttributeAs <int>("width");
                    chunk.Height   = xChunk.GetAttributeAs <int>("height");

                    // Each chunk is a separate game object on the super layer
                    GameObject goChunk = new GameObject(string.Format("Chunk ({0},{1})", chunk.X, chunk.Y));
                    goLayer.AddChildWithUniqueName(goChunk);

                    // Possition the chunk
                    Vector3Int int3      = m_MapComponent.TilePositionToGridPosition(chunk.X, chunk.Y);
                    Vector3    translate = SuperImportContext.MakePoint(int3.x, int3.y);
                    translate.x *= m_MapComponent.CellSize.x;
                    translate.y *= m_MapComponent.CellSize.y;
                    goChunk.transform.localPosition = translate;

                    // Create the tilemap for the layer if needed
                    if (!tilesAsObjects)
                    {
                        var tilemap = goChunk.AddComponent <Tilemap>();
                        tilemap.tileAnchor         = Vector3.zero;
                        tilemap.animationFrameRate = AnimationFramerate;
                        tilemap.color = new Color(1, 1, 1, superComp.CalculateOpacity());

                        // Create the renderer for the layer
                        var renderer = goChunk.AddComponent <TilemapRenderer>();
                        renderer.sortOrder = MapRenderConverter.Tiled2Unity(m_MapComponent.m_RenderOrder);
                        AssignSortingLayer(renderer, superComp.m_SortingLayerName, superComp.m_SortingOrder);
                    }

                    ProcessLayerDataChunk(goChunk, chunk);
                }
            }
            else
            {
                // Regular maps only have one chunk with the Tilemap and TileRenderer being on the layer object

                // Add the tilemap components if needed
                if (!tilesAsObjects)
                {
                    var tilemap = goLayer.AddComponent <Tilemap>();
                    tilemap.tileAnchor         = Vector3.zero;
                    tilemap.animationFrameRate = AnimationFramerate;
                    tilemap.color = new Color(1, 1, 1, superComp.CalculateOpacity());

                    // Create the renderer for the layer
                    var renderer = goLayer.AddComponent <TilemapRenderer>();
                    renderer.sortOrder = MapRenderConverter.Tiled2Unity(m_MapComponent.m_RenderOrder);
                    AssignSortingLayer(renderer, superComp.m_SortingLayerName, superComp.m_SortingOrder);
                }

                // For regular maps the 'chunk' is the same as the layer data
                chunk.XmlChunk = xData;
                chunk.X        = 0;
                chunk.Y        = 0;
                chunk.Width    = m_MapComponent.m_Width;
                chunk.Height   = m_MapComponent.m_Height;

                ProcessLayerDataChunk(goLayer, chunk);
            }
        }
 public void BeginTileLayer(SuperTileLayer layer)
 {
     SortingLayerCheck(layer.gameObject);
 }
        private void ProcessLayerData(GameObject goLayer, XElement xData)
        {
            Assert.IsNotNull(goLayer);
            Assert.IsNotNull(goLayer.GetComponent <SuperTileLayer>());
            Assert.IsNotNull(xData);

            SuperTileLayer superComp = goLayer.GetComponent <SuperTileLayer>();

            var chunk = new Chunk();

            chunk.Encoding    = xData.GetAttributeAs <DataEncoding>("encoding");
            chunk.Compression = xData.GetAttributeAs <DataCompression>("compression");

            // Are we reading in data in smaller chunks (for infinite maps) or one big chunk (the full map)
            var xChunks = xData.Elements("chunk");

            if (xChunks.Any())
            {
                foreach (var xChunk in xChunks)
                {
                    chunk.XmlChunk = xChunk;
                    chunk.X        = xChunk.GetAttributeAs <int>("x");
                    chunk.Y        = xChunk.GetAttributeAs <int>("y");
                    chunk.Width    = xChunk.GetAttributeAs <int>("width");
                    chunk.Height   = xChunk.GetAttributeAs <int>("height");

                    // Each chunk is a separate game object on the super layer
                    GameObject goChunk = new GameObject(string.Format("Chunk ({0},{1})", chunk.X, chunk.Y));
                    goLayer.AddChildWithUniqueName(goChunk);

                    // Position the chunk
                    Vector2 translate = m_MapComponent.MapCoordinatesToPositionPPU(chunk.X, chunk.Y);
                    goChunk.transform.localPosition = translate;

                    // Create the tilemap for the layer if needed
                    if (!m_TilesAsObjects)
                    {
                        GetOrAddTilemapComponent(goChunk, superComp);
                    }

                    ProcessLayerDataChunk(goChunk, chunk);
                }
            }
            else
            {
                // Regular maps only have one chunk with the Tilemap and TileRenderer being on the layer object

                // Add the tilemap components if needed
                if (!m_TilesAsObjects)
                {
                    GetOrAddTilemapComponent(goLayer, superComp);
                }

                // For regular maps the 'chunk' is the same as the layer data
                chunk.XmlChunk = xData;
                chunk.X        = 0;
                chunk.Y        = 0;
                chunk.Width    = m_MapComponent.m_Width;
                chunk.Height   = m_MapComponent.m_Height;

                ProcessLayerDataChunk(goLayer, chunk);
            }
        }