- Home /
Question by
moscacrackreina · Jan 07 at 09:46 PM ·
scripting problemsprite
Creating a sprite composed of sprites on runtime
I attempt to make a map like rimworld, a texture created from box sprites, and I can't make it work. Unity doesnt tell me the problem and when I try to run it, it doesn't stop and continues in a large loop. This is my code:
public class TextureCreator : MonoBehaviour
{
public int MapScale = 2;
[SerializeField] private SpriteRenderer spriteRenderer = null;
[SerializeField] private int pixelSprite;
public Sprite sprite;
public Color color;
void Start()
{
Texture2D newTex = new Texture2D(MapScale * pixelSprite, MapScale * pixelSprite);
for (int x = 0; x < newTex.width; x++)
{
for (int y = 0; y < newTex.width; x++)
{
newTex.SetPixel(x, y, new Color(1, 1, 1, 0));
}
}
for (int x = 0; x < MapScale; x++)
{
for (int y = 0; y < MapScale; x++)
{
for (int z = 0; z < pixelSprite; z++)
{
for (int w = 0; w < pixelSprite; w++)
{
Color color = sprite.texture.GetPixel(z, w);
newTex.SetPixel(x * pixelSprite + z, y * pixelSprite + w, color);
}
}
}
}
newTex.Apply();
Sprite finalSprite = Sprite.Create(newTex, new Rect(0, 0, newTex.width, newTex.height), new Vector2(0.5f, 0.5f));
finalSprite.name = "NewSprite";
spriteRenderer.sprite = finalSprite;
}
}
Comment