Пример #1
0
        static void CreateSingleImageTilesetPaths(TSX.Tileset tileset, string pathWithoutFile, string tilesetSpriteTargetDir, string tilesetTileTargetDir, out string imageSourcePath, out string imageTargetPath, out string[] tileTargetPaths)
        {
            imageSourcePath = tileset.image.source;
            imageSourcePath = Path.Combine(pathWithoutFile, imageSourcePath);

            string imageName = Path.GetFileName(imageSourcePath);

            imageTargetPath = Path.Combine(tilesetSpriteTargetDir, imageName);

            int tileCount = tileset.GetTileCount();

            tileTargetPaths = new string[tileCount];
            for (int i = 0; i < tileCount; i++)
            {
                string tileName = Path.GetFileNameWithoutExtension(imageSourcePath) + "_" + i + ".asset";
                tileTargetPaths[i] = Path.Combine(tilesetTileTargetDir, tileName);
            }
        }
Пример #2
0
        static bool CreateTilemapSprite(string targetPath, int cellWidth, int cellHeight, int pixelsPerUnit, TSX.Tileset tileset, string subSpriteNameBase, out Sprite[] tileSprites)
        {
            TextureImporter ti = AssetImporter.GetAtPath(targetPath) as TextureImporter;

            if (ti == null)
            {
                tileSprites = new Sprite[0];
                Debug.LogError("Cannot find tilemap sprite file at " + targetPath);
                return(false);
            }

            TextureImporterSettings textureSettings = new TextureImporterSettings();

            ti.ReadTextureSettings(textureSettings);

            SpriteMeshType   meshType             = SpriteMeshType.FullRect;
            SpriteAlignment  alignment            = SpriteAlignment.Custom;
            Vector2          pivot                = new Vector2(0.5f, 0.5f); // For the whole tileset sprite, so not important
            FilterMode       filterMode           = FilterMode.Point;
            SpriteImportMode importMode           = SpriteImportMode.Multiple;
            bool             fallbackPhysicsShape = true;

            if (textureSettings.spritePixelsPerUnit != pixelsPerUnit ||
                textureSettings.spriteMeshType != meshType ||
                textureSettings.spriteAlignment != (int)alignment ||
                textureSettings.spritePivot != pivot ||
                textureSettings.filterMode != filterMode ||
                textureSettings.spriteMode != (int)importMode ||
                textureSettings.spriteGenerateFallbackPhysicsShape != fallbackPhysicsShape)
            {
                textureSettings.spritePixelsPerUnit = pixelsPerUnit;
                textureSettings.spriteMeshType      = meshType;
                textureSettings.spriteAlignment     = (int)alignment;
                textureSettings.spritePivot         = pivot;
                textureSettings.filterMode          = filterMode;
                textureSettings.spriteMode          = (int)importMode;
                textureSettings.spriteGenerateFallbackPhysicsShape = fallbackPhysicsShape;

                ti.SetTextureSettings(textureSettings);
                int tileCount = tileset.GetTileCount();

                List <SpriteMetaData> newData = new List <SpriteMetaData>(tileCount);

                int i = 0;
                for (int y = tileset.image.height - tileset.margin; (y - tileset.tileheight >= tileset.margin); y -= tileset.tileheight + tileset.spacing)
                {
                    for (int x = tileset.margin; x + tileset.tilewidth <= tileset.image.width - tileset.margin; x += tileset.tilewidth + tileset.spacing)
                    {
                        SpriteMetaData data = new SpriteMetaData();
                        data.name      = subSpriteNameBase + "_" + i;
                        data.alignment = (int)alignment;
                        data.pivot     = GetPivot(tileset.tilewidth, tileset.tileheight, cellWidth, cellHeight);
                        data.rect      = new Rect(x, y - tileset.tileheight, tileset.tilewidth, tileset.tileheight);

                        newData.Add(data);
                        i++;
                        if (i >= tileCount)
                        {
                            break;
                        }
                    }
                    if (i >= tileCount)
                    {
                        break;
                    }
                }

                ti.spritesheet = newData.ToArray();

                EditorUtility.SetDirty(ti);
                ti.SaveAndReimport();
            }

            Sprite[] subSprites = AssetDatabase.LoadAllAssetsAtPath(targetPath).OfType <Sprite>().ToArray();
            // For some reason Unity thinks it's smart to return the sub-sprites in random order...
            // ...and provide no API for retrieving sub-sprites by index >_<
            // so we have to manually sort them by the ids in their names
            Array.Sort <Sprite>(subSprites, new SpriteComparer());
            tileSprites = subSprites;
            return(true);
        }