【问题标题】:How do I split an image in unity [duplicate]如何统一分割图像[重复]
【发布时间】:2026-01-18 13:30:01
【问题描述】:

我想用 c# 为这张图片制作动画。精灵编辑器对我不起作用,因为我希望它是一个动态系统。 如果有这种方法,如果您与我分享,我将不胜感激。

【问题讨论】:

  • 我认为你走得很辛苦,但如果你真的想这样做,你可以计算你想要使用的区域,通过new Texture2D(width, height)创建新纹理并将其分配给Sprite
  • 有多个这样的图像,我认为为每个图像创建一个精灵是没有意义的。我想把这张图片用代码分割成动态的。
  • 见@MarkSouls 评论。是的,如果您事先知道 Rect 像素大小,您可以从给定的纹理中剪切纹理或精灵。但是,如果您正在寻找适用于任何给定图像比例的动态...不,这将非常棘手^^所以..都是相同尺寸的输入图像,您是否总是希望将它们分成相同大小的部分?还有右边那个空白处呢?
  • 所有图像的间距和大小都相同。我想分割相同大小的图像。
  • 这能回答你的问题吗? How to slice sprite by script ?(not use Editor)

标签: unity3d unity-webgl


【解决方案1】:

如果如您所说,所有源和目标平铺图像都具有已知尺寸,您可以这样做,例如

如果您需要它们为Texture2Ds(更昂贵且内存密集)

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;
} 

或者如果你需要他们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;
} 

【讨论】: