Beispiel #1
0
        public static TMXFile Load(XmlTextReader xmlReader, string path)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(TMXFile));
            TMXFile       map          = (TMXFile)deserializer.Deserialize(xmlReader);

            bool hasDocType = false;

            for (int i = 0; i < 3; i++)
            {
                xmlReader.Read();
                if (xmlReader.NodeType == XmlNodeType.DocumentType)
                {
                    hasDocType = true;
                    break;
                }
            }
            map.hasDocType = hasDocType;
            xmlReader.Close();

            if (map.tileSets == null)
            {
                return(map);
            }

            for (int i = 0; i < map.tileSets.Length; i++)
            {
                TileSet tileSet = map.tileSets[i];
                if (tileSet.hasSource)
                {
                    string tsxPath = tileSet.source;
                    tsxPath = Path.Combine(Path.GetDirectoryName(path), tsxPath);
                    tsxPath = Path.GetFullPath(tsxPath);
                    TileSet tsxFile = TileSet.Load(tsxPath);

                    tsxFile.firstGID = tileSet.firstGID;
                    tsxFile.source   = tileSet.source;
                    tileSet          = tsxFile;
                }

                if ((tileSet.columns == 0 || tileSet.tileCount == 0) && tileSet.image != null)
                {
                    tileSet.columns = (tileSet.image.width - 2 * tileSet.margin + tileSet.spacing) / (tileSet.tileWidth + tileSet.spacing);
                    tileSet.rows    = (tileSet.image.height - 2 * tileSet.margin + tileSet.spacing) / (tileSet.tileHeight + tileSet.spacing);
                }
                map.tileSets[i] = tileSet;
            }
            return(map);
        }
Beispiel #2
0
        private void UpdateTexturePadding()
        {
            tileSets[path] = TileSet.Load(path); // DELETE ME
            TileSet   ts          = tileSet;
            Texture2D texture     = TileMapEditor.GetTileSetTexture(ts, path);
            string    texturePath = AssetDatabase.GetAssetPath(texture);

            TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(texturePath);

            importer.isReadable = true;
            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

            int inWidth   = ts.tileWidth * ts.columns + ts.margin * 2 + ts.spacing * (ts.columns - 1);
            int outWidth  = ts.tileWidth * ts.columns + margin * 2 + spacing * (ts.columns - 1);
            int outHeight = ts.tileHeight * ts.rows + margin * 2 + spacing * (ts.rows - 1);

            Color[] inColors  = texture.GetPixels();
            Color[] outColors = new Color[outWidth * outHeight];

            for (int column = 0; column < ts.columns; column++)
            {
                for (int row = 0; row < ts.rows; row++)
                {
                    for (int i = 0; i < ts.tileWidth; i++)
                    {
                        for (int j = 0; j < ts.tileHeight; j++)
                        {
                            int   inX = i + (ts.tileWidth + ts.spacing) * column + ts.margin;
                            int   inY = j + (ts.tileHeight + ts.spacing) * row + ts.margin;
                            Color c   = inColors[inY * inWidth + inX];

                            int left  = (i == 0) ? ((column == 0) ? -margin : -spacing / 2) : 0;
                            int right = (i == ts.tileWidth - 1) ? ((column == ts.columns - 1) ? margin : spacing / 2) : 0;
                            int down  = (j == 0) ? ((row == 0) ? -margin : -spacing / 2) : 0;
                            int up    = (j == ts.tileHeight - 1) ? ((row == ts.rows - 1) ? margin : spacing / 2) : 0;
                            for (int x = i + left; x <= i + right; x++)
                            {
                                for (int y = j + down; y <= j + up; y++)
                                {
                                    int outX = x + (ts.tileWidth + spacing) * column + margin;
                                    int outY = y + (ts.tileHeight + spacing) * row + margin;
                                    outColors[outY * outWidth + outX] = c;
                                }
                            }
                        }
                    }
                }
            }

            Texture2D t = new Texture2D(outWidth, outHeight);

            t.SetPixels(outColors);
            byte[] bytes = t.EncodeToPNG();
            File.WriteAllBytes(texturePath, bytes);

            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

            tileSet.margin       = margin;
            tileSet.spacing      = spacing;
            tileSet.image.width  = outWidth;
            tileSet.image.height = outHeight;
            tileSet.Save(path);
        }