-3

enter image description here

I want to animate this image using c #. The sprite editor doesn't work for me because I want it to be a dynamic system. If there is a method for this, I would appreciate it if you share it with me.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 3
    I think you're going the hard way, but if you really want to do it you can calculate area you want to use, create new texture by ```new Texture2D(width, height)``` and assign it to ```Sprite```. – MarkSouls May 21 '21 at 07:06
  • There are multiple images like this, I thought it wouldn't make sense to create a sprite for each. I want to make this picture dynamic by dividing it with code. – Mert Karca May 21 '21 at 07:16
  • See @MarkSouls comment. Yes, if you know the Rect pixel sizes beforehand you can cut textures or sprites out of your given texture. But if you are looking for something dynamic which works for any given image scales ... Nope this will be quite tricky ^^ so .. are all input images of the same dimensions and do you always want to split them into the same sized portions? Also what about that blanc space on the right? – derHugo May 21 '21 at 07:16
  • The gaps and sizes of all the images are the same. I want to split images of the same size. – Mert Karca May 21 '21 at 07:22
  • 3
    Does this answer your question? [How to slice sprite by script ?(not use Editor)](https://stackoverflow.com/questions/55738954/how-to-slice-sprite-by-script-not-use-editor) – Nikolay Gonza May 21 '21 at 07:37
  • @MertKarca do you need them as `Sprite` or as `Texture2D`? A `Sprite` would have the advantage that you can keep the original `Texture2D` asset and don't have to create multiple `Texture2D` instances -> needs more memory. – derHugo May 21 '21 at 07:53
  • If you are building a game and not a tool, you seriously need to consider what @MarkSouls said above. If you are making a tool however... Well, first of all, good luck appeasing the assetstore gods. Now, for your consideration, you need to make sure that the user is inserting the proper size image (that can be a huge pain, because users rarelly read the guidelines). Then, you can think about how to crop each sprite to a different one. Good luck. – Rhach May 21 '21 at 08:00
  • yes I am developing a game. Do you think the easiest way is to use a sprite editor? Because there are hundreds of images. – Mert Karca May 21 '21 at 08:04

1 Answers1

1

If as you say all the source and target tile images have known dimensions you can do e.g.

If you need them as Texture2Ds (more expensive and memory intense)

public Texture2D[] SplitTextureToTextures(Texture2D source, Vector2Int tileSize)
{
    // get the source dimensions
    var sourceWidth = source.width;
    var sourceHeight = source.height;

    // Check how often the given tileSize fits into the image
    var tileAmountX = Mathf.FloorToInt(sourceWidth / (float)tileSize.x);
    var tileAmountY = Mathf.FloorToInt(sourceHeight / (float)tileSize.y);

    var output = new Texture2D[tileAmountX * tileAmountY];

    // Iterate through the tiles horizontal then vertical
    // starting at the top left tile
    for (var y = tileAmountY - 1; y >= 0; y--)
    {
        for (var x = 0; x < tileAmountX; x++)
        {
            // get the bottom left pixel coordinate of the current tile
            var bottomLeftPixelX = x * tileSize.x;
            var bottomLeftPixelY = y * tileSize.y;

            // get the pixels in the rect for this tile
            var pixels = source.GetPixels(bottomLeftPixelX, bottomLeftPixelY, tileSize.x, tileSize.y);

            // create the new texture (adjust the additional parameters according to your needs)
            var texture = new Texture2D(tileSize.x, tileSize.y, source.format, false);
                
            // write the pixels into the new texture
            texture.SetPixels(pixels);
            texture.Apply();

            // and store it in the output
            output[x + y * tileAmountX] = texture;
        }
    }

    return output;
} 

or if you need them as Sprites

public Sprite[] SplitTextureToSprites(Texture2D source, Vector2Int tileSize)
{
    // get the source dimensions
    var sourceWidth = source.width;
    var sourceHeight = source.height;

    // Check how often the given tileSize fits into the image
    var tileAmountX = Mathf.FloorToInt(sourceWidth / (float)tileSize.x);
    var tileAmountY = Mathf.FloorToInt(sourceHeight / (float)tileSize.y);

    var output = new Texture2D[tileAmountX * tileAmountY];

    // Iterate through the tiles horizontal then vertical
    // starting at the top left tile
    for (var y = tileAmountY - 1; y >= 0; y--)
    {
        for (var x = 0; x < tileAmountX; x++)
        {
           // get the bottom left pixel coordinate of the current tile
            var bottomLeftPixelX = x * tileSize.x;
            var bottomLeftPixelY = y * tileSize.y;

            // instead of having to get the pixel data
            // For a sprite you just have to set the rect from which part of the texture to create a sprite
            var sprite = Sprite.Create(source, new Rect(bottomLeftPixelX, bottomLeftPixelY, tileSize.x, tileSize.y), Vector2.one * 0.5f);

            output[x + y * tileAmountX] = sprite;
        }
    }

    return output;
} 
derHugo
  • 83,094
  • 9
  • 75
  • 115