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 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); } }