Exemplo n.º 1
0
        // replaces color to transparent
        public Texture2D GenerateAtlas(Texture2D[] sprites, out UnityEditor.AssetImporters.SpriteImportData[] spriteData, int cols, int rows,
                                       Color mask, bool baseTwo = true)
        {
            var spriteImportData = new List <UnityEditor.AssetImporters.SpriteImportData>();

            var width  = cols * spriteSize.x;
            var height = rows * spriteSize.y;

            if (baseTwo)
            {
                var baseTwoValue = CalculateNextBaseTwoValue(Math.Max(width, height));
                width  = baseTwoValue;
                height = baseTwoValue;
            }

            // blank transparent canvas
            var atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            var index = 0;

            // step through each pixel
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Rect spriteRect = new Rect(col * spriteSize.x, atlas.height - ((row + 1) * spriteSize.y),
                                               spriteSize.x, spriteSize.y);

                    // change pixel mask to transparent
                    Color[] pixelPallete = ReplaceMaskToTransparent(mask, sprites[index].GetPixels());
                    atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width,
                                    (int)spriteRect.height, pixelPallete);
                    atlas.Apply();

                    var importData = new UnityEditor.AssetImporters.SpriteImportData
                    {
                        rect   = spriteRect,
                        pivot  = textureSettings.spritePivot,
                        border = Vector4.zero,
                        name   = index.ToString()
                    };

                    spriteImportData.Add(importData);

                    index++;
                    if (index >= sprites.Length)
                    {
                        break;
                    }
                }

                if (index >= sprites.Length)
                {
                    break;
                }
            }

            spriteData = spriteImportData.ToArray();
            return(atlas);
        }
Exemplo n.º 2
0
        public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx)
        {
            name = GetFileName(ctx.assetPath);

            AseFile aseFile    = ReadAseFile(ctx.assetPath);
            int     frameCount = aseFile.Header.Frames;

            SpriteAtlasBuilder atlasBuilder = new SpriteAtlasBuilder(textureSettings, aseFile.Header.Width, aseFile.Header.Height);

            Texture2D[] frames = null;
            if (importType != AseFileImportType.LayerToSprite)
            {
                frames = aseFile.GetFrames();
            }
            else
            {
                frames = aseFile.GetLayersAsFrames();
            }

            UnityEditor.AssetImporters.SpriteImportData[] spriteImportData = new UnityEditor.AssetImporters.SpriteImportData[0];

            //if (textureSettings.transparentMask)
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentColor, false);
            //}
            //else
            //{
            //    atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, false);

            //}

            atlas = atlasBuilder.GenerateAtlas(frames, out spriteImportData, textureSettings.transparentMask, false);


            atlas.filterMode          = textureSettings.filterMode;
            atlas.alphaIsTransparency = false;
            atlas.wrapMode            = TextureWrapMode.Clamp;
            atlas.name = "Texture";

            ctx.AddObjectToAsset("Texture", atlas);

            ctx.SetMainObject(atlas);

            switch (importType)
            {
            case AseFileImportType.LayerToSprite:
            case AseFileImportType.Sprite:
                ImportSprites(ctx, aseFile, spriteImportData);
                break;

            case AseFileImportType.Tileset:
                ImportTileset(ctx, atlas);
                break;
            }

            ctx.SetMainObject(atlas);
        }
Exemplo n.º 3
0
        private static Sprite CreateNamedSprite(
            string name,
            Texture2D texture,
            UnityEditor.AssetImporters.SpriteImportData importData,
            AsepriteImporterSettings settings)
        {
            var sprite = Sprite.Create(
                texture,
                importData.rect,
                importData.pivot,
                settings.PixelsPerUnit,
                settings.ExtrudeEdges,
                settings.MeshType,
                importData.border,
                settings.GeneratePhysicsShape);

            sprite.name = name;
            return(sprite);
        }
Exemplo n.º 4
0
        public Texture2D GenerateAtlas(Texture2D[] sprites, out UnityEditor.AssetImporters.SpriteImportData[] spriteData, int cols, int rows,
                                       bool baseTwo = true)
        {
            var spriteImportData = new List <UnityEditor.AssetImporters.SpriteImportData>();

            var width  = cols * spriteSize.x;
            var height = rows * spriteSize.y;

            if (baseTwo)
            {
                var baseTwoValue = CalculateNextBaseTwoValue(Math.Max(width, height));
                width  = baseTwoValue;
                height = baseTwoValue;
            }

            var atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            var index = 0;

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Rect spriteRect = new Rect(col * spriteSize.x, atlas.height - ((row + 1) * spriteSize.y),
                                               spriteSize.x, spriteSize.y);

                    switch (textureSettings.mirror)
                    {
                    case MirrorOption.X:
                        sprites[index] = FlipAtlas(sprites[index]);
                        break;

                    case MirrorOption.Y:
                        sprites[index] = FlipAtlas(sprites[index], true);
                        break;
                    }

                    atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width,
                                    (int)spriteRect.height, sprites[index].GetPixels());

                    atlas.Apply();

                    var importData = new UnityEditor.AssetImporters.SpriteImportData
                    {
                        rect   = spriteRect,
                        pivot  = textureSettings.spritePivot,
                        border = Vector4.zero,
                        name   = index.ToString()
                    };

                    spriteImportData.Add(importData);

                    index++;
                    if (index >= sprites.Length)
                    {
                        break;
                    }
                }

                if (index >= sprites.Length)
                {
                    break;
                }
            }

            spriteData = spriteImportData.ToArray();
            return(atlas);
        }
Exemplo n.º 5
0
        public AsepriteAssets CreateAssets(AsepriteImporterSettings settings, string name)
        {
            var padding         = settings.InnerPadding;
            var paddedFrameSize = new Vector2Int(
                _frameSize.x + padding * 2,
                _frameSize.y + padding * 2);

            var spritesheet = CreateBlankSpritesheet(
                settings,
                paddedFrameSize,
                _frames.Count,
                out int cols);

            var sprites = new List <Sprite>();

            for (var i = 0; i < _frames.Count; ++i)
            {
                var frame = _frames[i];
                var col   = i % cols;
                var row   = Mathf.FloorToInt(i / (float)cols) + 1;

                var spriteRect = new Rect(
                    col * paddedFrameSize.x,
                    spritesheet.height - (row * paddedFrameSize.y),
                    paddedFrameSize.x,
                    paddedFrameSize.y);

                var framePixels = frame.GetFramePixels(_frameSize);
                spritesheet.SetPixels(
                    (int)spriteRect.x + padding,
                    (int)spriteRect.y + padding,
                    _frameSize.x,
                    _frameSize.y,
                    framePixels);

                var spriteData = new UnityEditor.AssetImporters.SpriteImportData
                {
                    rect  = spriteRect,
                    pivot = settings.PivotValue
                            + new Vector2(
                        padding / (float)paddedFrameSize.x,
                        padding / (float)paddedFrameSize.y),
                    name = i.ToString()
                };

                var sprite = CreateNamedSprite(
                    string.Format("{0}_{1}", name, spriteData.name),
                    spritesheet,
                    spriteData,
                    settings);

                sprites.Add(sprite);
            }

            spritesheet.Apply();

            var animations   = CreateAnimations(name, sprites, typeof(SpriteRenderer), settings.AnimationLoop);
            var uiAnimName   = $"UI_{name}";
            var uiAnimations = CreateAnimations(uiAnimName, sprites, typeof(Image), settings.AnimationLoop);

            return(new AsepriteAssets(
                       spritesheet,
                       sprites,
                       animations.Concat(uiAnimations)));
        }