Я хочу анимировать это изображение с помощью c #. Редактор спрайтов у меня не работает, потому что я хочу, чтобы это была динамическая система. Если есть метод для этого, я был бы признателен, если бы вы поделиться им со мной.
Как разделить изображение в единстве
Ответы (1)
Если, как вы говорите, все исходные и целевые изображения плитки имеют известные размеры, вы можете сделать, например.
Если они вам нужны как Texture2D
s (более дорогие и интенсивные по памяти)
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;
}
или если они вам нужны как Sprite
s
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;
}
person
derHugo
schedule
21.05.2021
new Texture2D(width, height)
и назначить ееSprite
. - person MarkSouls   schedule 21.05.2021Sprite
или какTexture2D
? ПреимуществоSprite
состоит в том, что вы можете сохранить исходный активTexture2D
и не создавать несколько экземпляровTexture2D
-> требуется больше памяти. - person derHugo   schedule 21.05.2021