private void BuildTilesetFromCollection(XElement xTileset, AtlasBuilder atlas) { m_TilesetScript.m_IsImageCollection = true; foreach (var xTile in xTileset.Elements("tile")) { int tileIndex = xTile.GetAttributeAs <int>("id"); var xImage = xTile.Element("image"); if (xImage != null) { string textureAssetPath = xImage.GetAttributeAs <string>("source"); // Load the texture. We will make sprites and tiles out of this image. var tex2d = m_Importer.RequestAssetAtPath <Texture2D>(textureAssetPath); if (tex2d == null) { // Texture was not found yet so report the error to the importer UI and bail m_Importer.ReportError("Missing texture asset for tile {0}: {1}", tileIndex, textureAssetPath); return; } var rcSource = new Rect(0, 0, tex2d.width, tex2d.height); atlas.AddTile(tileIndex, tex2d, rcSource); } } }
private void BuildTilesetFromImage(XElement xTileset, AtlasBuilder atlas) { m_TilesetScript.m_IsImageCollection = false; XElement xImage = xTileset.Element("image"); string textureAssetPath = xImage.GetAttributeAs <string>("source"); int textureWidth = xImage.GetAttributeAs <int>("width"); int textureHeight = xImage.GetAttributeAs <int>("height"); // Load the texture. We will make sprites and tiles out of this image. var tex2d = m_Importer.RequestAssetAtPath <Texture2D>(textureAssetPath); if (tex2d == null) { // Texture was not found so report the error to the importer UI and bail m_Importer.ReportError("Missing texture asset: {0}", textureAssetPath); m_TilesetScript.m_HasErrors = true; return; } if (tex2d.width < textureWidth || tex2d.height < textureHeight) { // Texture was not imported into Unity correctly var max = Mathf.Max(textureWidth, textureHeight); m_Importer.ReportError("Texture was imported at a smaller size. Make sure 'Max Size' on '{0}' is at least '{1}'", textureAssetPath, max); m_TilesetScript.m_HasErrors = true; return; } for (int i = 0; i < m_TilesetScript.m_TileCount; i++) { // Get grid x,y coords int x = i % m_TilesetScript.m_TileColumns; int y = i / m_TilesetScript.m_TileColumns; // Get x source on texture int srcx = x * m_TilesetScript.m_TileWidth; srcx += x * m_TilesetScript.m_Spacing; srcx += m_TilesetScript.m_Margin; // Get y source on texture int srcy = y * m_TilesetScript.m_TileHeight; srcy += y * m_TilesetScript.m_Spacing; srcy += m_TilesetScript.m_Margin; // In Tiled, texture origin is the top-left. However, in Unity the origin is bottom-left. srcy = (textureHeight - srcy) - m_TilesetScript.m_TileHeight; // Add the tile to our atlas Rect rcSource = new Rect(srcx, srcy, m_TilesetScript.m_TileWidth, m_TilesetScript.m_TileHeight); atlas.AddTile(i, tex2d, rcSource); } }
private void BuildTileset(XElement xTileset) { // Build the initial database of tiles and the image components that make them // There are two ways that our collection of tiles can be created from images // 1) From one image broken down into parts (many tiles in one image) // 2) From a collection of images (one tile per image) var atlas = new AtlasBuilder(m_Importer, m_UseSpriteAtlas, (int)m_AtlasWidth, (int)m_AtlasHeight, m_TilesetScript); if (xTileset.Element("image") != null) { BuildTilesetFromImage(xTileset, atlas); } else { BuildTilesetFromCollection(xTileset, atlas); } // We're done collecting all the tile data. Build our atlas. // (Note that we call build even if we are not using texture atlases) atlas.Build(); }
private void BuildTilesetFromCollection(XElement xTileset, AtlasBuilder atlas) { m_TilesetScript.m_IsImageCollection = true; foreach (var xTile in xTileset.Elements("tile")) { int tileIndex = xTile.GetAttributeAs <int>("id"); var xImage = xTile.Element("image"); if (xImage != null) { string textureAssetPath = xImage.GetAttributeAs <string>("source"); int textureWidth = xImage.GetAttributeAs <int>("width"); int textureHeight = xImage.GetAttributeAs <int>("height"); // Load the texture. We will make sprites and tiles out of this image. var tex2d = m_Importer.RequestAssetAtPath <Texture2D>(textureAssetPath); if (tex2d == null) { // Texture was not found yet so report the error to the importer UI and bail m_Importer.ReportError("Missing texture asset for tile {0}: {1}", tileIndex, textureAssetPath); m_TilesetScript.m_HasErrors = true; return; } if (tex2d.width < textureWidth || tex2d.height < textureHeight) { // Texture was not imported into Unity correctly var max = Mathf.Max(textureWidth, textureHeight); m_Importer.ReportError("Texture was imported at a smaller size. Make sure 'Max Size' on '{0}' is at least '{1}'", textureAssetPath, max); m_TilesetScript.m_HasErrors = true; return; } var rcSource = new Rect(0, 0, tex2d.width, tex2d.height); atlas.AddTile(tileIndex, tex2d, rcSource); } } }
private void BuildTilesetFromImage(XElement xTileset, AtlasBuilder atlas) { m_TilesetScript.m_IsImageCollection = false; XElement xImage = xTileset.Element("image"); string textureAssetPath = xImage.GetAttributeAs <string>("source"); int textureWidth = xImage.GetAttributeAs <int>("width"); int textureHeight = xImage.GetAttributeAs <int>("height"); // Load the texture. We will make sprites and tiles out of this image. var tex2d = m_Importer.RequestAssetAtPath <Texture2D>(textureAssetPath); if (tex2d == null) { // Texture was not found so report the error to the importer UI and bail m_Importer.ReportError("Missing texture asset: {0}", textureAssetPath); m_TilesetScript.m_HasErrors = true; return; } // This is annoying but a tileset may have recently changed but Tiled hasn't been updated on the status yet var texAssetPath = AssetDatabase.GetAssetPath(tex2d); var texFullPath = Path.GetFullPath(texAssetPath); var imgHeaderDims = ImageHeader.GetDimensions(texFullPath); if (imgHeaderDims.x != textureWidth || imgHeaderDims.y != textureHeight) { // Tileset needs to be resaved in Tiled m_Importer.ReportError("Mismatching width/height detected. Tileset = ({0}, {1}), image = {2}. This may happen when a tileset image has been resized. Open map and tileset in Tiled Map Editor and resave.", textureWidth, textureHeight, imgHeaderDims); m_TilesetScript.m_HasErrors = true; return; } if (tex2d.width < textureWidth || tex2d.height < textureHeight) { // Texture was not imported into Unity correctly var max = Mathf.Max(textureWidth, textureHeight); m_Importer.ReportError("Texture was imported at a smaller size. Make sure 'Max Size' on '{0}' is at least '{1}'", textureAssetPath, max); m_TilesetScript.m_HasErrors = true; return; } for (int i = 0; i < m_TilesetScript.m_TileCount; i++) { // Get grid x,y coords int x = i % m_TilesetScript.m_TileColumns; int y = i / m_TilesetScript.m_TileColumns; // Get x source on texture int srcx = x * m_TilesetScript.m_TileWidth; srcx += x * m_TilesetScript.m_Spacing; srcx += m_TilesetScript.m_Margin; // Get y source on texture int srcy = y * m_TilesetScript.m_TileHeight; srcy += y * m_TilesetScript.m_Spacing; srcy += m_TilesetScript.m_Margin; // In Tiled, texture origin is the top-left. However, in Unity the origin is bottom-left. srcy = (textureHeight - srcy) - m_TilesetScript.m_TileHeight; if (srcy < 0) { // This is an edge condition in Tiled if a tileset's texture may have been resized break; } // Add the tile to our atlas Rect rcSource = new Rect(srcx, srcy, m_TilesetScript.m_TileWidth, m_TilesetScript.m_TileHeight); atlas.AddTile(i, tex2d, rcSource); } }