public static Tileset Load(AssetImportContext context, TSX tsx) { // Image collection tilesets can have gaps in their IDs; use the highest ID instead. var tilecount = Mathf.Max(tsx.tilecount, tsx.tiles.LastOrDefault().id + 1); // Texture Texture2D texture = null; var columns = 0; var rows = 0; if (tsx.image.source == null) { // Image collection tilesets don't have images or column counts; use a single row. columns = tilecount; rows = 1; } else { texture = LoadTexture(tsx.image.source, tsx.image.trans, context); // Column count might not exist, so just compute it instead. columns = (texture.width - tsx.margin) / (tsx.tilewidth + tsx.spacing); rows = (texture.height - tsx.margin) / (tsx.tileheight + tsx.spacing); // Tile count depends only on column and row count. tilecount = columns * rows; } // Single-image tilesets all use the same texture. var tiles = new (GameObject prefab, Texture2D texture, TSX.Tile.Object[] objects, TSX.Tile.Frame[] frames)[tilecount];
///<summary> ///Load embedded or external tileset. ///</summary> static Tileset ParseTileset(AssetImportContext context, TSX tsx) { // Load embedded tileset. if (tsx.source == null) { return(TSXImporter.Load(context, tsx)); } // Load external tileset, respecting relative paths. var source = PathHelper.AssetPath( Path.GetDirectoryName(context.assetPath) + Path.DirectorySeparatorChar + tsx.source ); context.DependsOnSourceAsset(source); return(AssetDatabase.LoadAssetAtPath <Tileset>(source)); }
public TSX(XElement element, string assetPath = "") { // Attributes firstgid = (int? )element.Attribute("firstgid") ?? 0; source = (string)element.Attribute("source"); if (!string.IsNullOrEmpty(source)) // External tileset { var tsx = new TSX(XDocument.Load(assetPath + source).Element("tileset")); name = tsx.name; tilewidth = tsx.tilewidth; tileheight = tsx.tileheight; spacing = tsx.spacing; margin = tsx.margin; tilecount = tsx.tilecount; columns = tsx.columns; tileoffset = tsx.tileoffset; image = tsx.image; tiles = tsx.tiles; return; } name = (string)element.Attribute("name"); tilewidth = (int )element.Attribute("tilewidth"); tileheight = (int )element.Attribute("tileheight"); spacing = (int? )element.Attribute("spacing") ?? 0; margin = (int? )element.Attribute("margin") ?? 0; tilecount = (int? )element.Attribute("tilecount") ?? 0; columns = (int? )element.Attribute("columns") ?? 0; // Elements tileoffset = new Tileoffset(element.Element("tileoffset")); image = new Image(element.Element("image")); tiles = element .Elements("tile") .Select(t => new Tile(t)) .OrderBy(t => t.id) .ToArray(); }