Generating a sprite from a script,Trying to generate a sprite from a script
So after a good 6 hours of trying, I'm at my wits end. I had a fun Idea that requires me to generate a simple sprite using script for later use. I tested out 3 ways to do it and none of them seem to work.
Making a new texture from scratch:
Code (CSharp):
Texture2D backgroundTexture = new Texture2D(backgroundWidth, backgroundHeight);
Rect backgroundRect = new Rect(0, 0, backgroundWidth, backgroundHeight);
for (int i = 0; i < backgroundWidth; i++)
{
for (int j = 0; j < backgroundHeight; j++)
{
backgroundTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
}
}
backgroundTexture.Apply();
Sprite backgroundSprite = Sprite.Create(backgroundTexture, backgroundRect, new Vector2(backgroundWidth / 2f, backgroundHeight / 2f), pixelsPerUnit);
backgroundSprite.name = "Background Sprite";
GetComponent<SpriteRenderer>().sprite = backgroundSprite;
I can inspect this sprite in the Editor when the program is running and even put pretty colorfull pixels whereever i want but i can't for the life of me get it to show up on camera.
Dropping a .png into the sprite renderer and trying to modify pixels:
Code (CSharp):
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
Sprite backgroundSprite = spriteRenderer.sprite;
Rect spriteRect = backgroundSprite.rect;
Texture2D spriteTexture = backgroundSprite.texture;
int backgroundHeight = Mathf.RoundToInt(spriteRect.height);
int backgroundWidth = Mathf.RoundToInt(spriteRect.width);
for (int i = 0; i < backgroundWidth; i++)
{
for (int j = 0; j < backgroundHeight; j++)
{
spriteTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
}
}
spriteTexture.Apply();
Apparently you can't do that because "the texture memory can not be accessed from scripts". Which is fine, i guess...
Loading an empty .bytes image from within the script and trying to modify:
Code (CSharp):
Texture2D backgroundTexture = new Texture2D(2, 2);
backgroundTexture.LoadImage(imageSize100.bytes);
Rect backgroundRect = new Rect(0, 0, size, size);
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
Sprite backgroundSprite = Sprite.Create(backgroundTexture, backgroundRect, new Vector2(size / 2f, size / 2f), pixelsPerUnit);
spriteRenderer.sprite = backgroundSprite;
//spriteRenderer.material.mainTexture = backgroundTexture;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
backgroundTexture.SetPixel(i, j, new Color(1f, 1f, 1f, 1f));
}
}
backgroundTexture.Apply();
This one too can be inspected in the small little bottom right window, but so far no luck on actually showing up on screen.
Anyone have any more Ideas? I'm usually pretty good at finding solutions myself using the documentation, but I am about ready to give up...
Edit: I realized that in the 1st and 3rd method something IS being displayed. with the first method it is a weird blue square at -5000, -5000. with the third it is a bright pink square at -5000, -5000. There must be an issue with how I am rendering the generated sprite rather than with the sprite itself. Can anyone help?