Hi, I have been working on a system that creates a texture for a platform by sampling different tiles from a single tileset. I have managed to get it working but the problem is that its very very slow. Just wandering if anyone has any ideas how I could speed things up or if there is any other approaches i should be looking into, thanks in advance.
void Generate () {
tileSize = texture.width / 4;
genTexture = new Texture2D(tileSize * (_sizeX + 1), tileSize * (_sizeY + 1));
for (int y = 0; y < _sizeY+1; y++) {
for (int x = 0; x < _sizeX+1; x++) {
int sourcePositionX = 0;
int sourcePositionY = 0;
//get source position
//single
if (_sizeX == 0 && _sizeY == 0) {
sourcePositionX = 3;
sourcePositionY = 3;
}
//block
if (_sizeX > 0 && _sizeY > 0) {
if (x == 0) {
sourcePositionX = 0;
} else if (x > 0 && x < _sizeX) {
sourcePositionX = 1;
} else if (x == _sizeX) {
sourcePositionX = 2;
}
if (y == 0) {
sourcePositionY = 0;
} else if (y > 0 && y < _sizeY) {
sourcePositionY = 1;
} else if (y == _sizeY) {
sourcePositionY = 2;
}
}
//long
if (sizeX > 0 && _sizeY == 0) {
sourcePositionY = 3;
if (x == 0) {
sourcePositionX = 0;
} else if (x > 0 && x < _sizeX) {
sourcePositionX = 1;
} else if (x == _sizeX) {
sourcePositionX = 2;
}
}
//tall
if (sizeY > 0 && _sizeX == 0) {
sourcePositionX = 3;
if (y == 0) {
sourcePositionY = 0;
} else if (y > 0 && y < _sizeY) {
sourcePositionY = 1;
} else if (y == _sizeY) {
sourcePositionY = 2;
}
}
//copy over data from source
Color[] pixelData = texture.GetPixels (sourcePositionX * tileSize, sourcePositionY * tileSize, tileSize, tileSize, 0);
genTexture.SetPixels (x * tileSize, y * tileSize, tileSize, tileSize, pixelData);
//adjust scale
transform.localScale = new Vector3(2f, 2f);
}
}
//create sprite from texture
genTexture.Apply ();
Sprite sprite = Sprite.Create (genTexture, new Rect(0, 0, genTexture.width, genTexture.height), new Vector2(0, 1));
spriteRenderer.sprite = sprite;
//set collision box
collider.size = new Vector2(sprite.bounds.size.x, sprite.bounds.size.y);
collider.center = new Vector2(sprite.bounds.center.x, sprite.bounds.center.y);
}