Unity generate a sprite with main camera dimentions
I need to create a sprite with code in my scene, but it should be exactly the size of the main camera view port. This is the code i have so far:
public class SpriteMaker : MonoBehaviour
{
private Sprite sprite;
private Texture2D texture;
void Start()
{
Camera cam = Camera.main;
int width = cam.pixelWidth;
int height = cam.pixelHeight;
texture = new Texture2D(width, height);
sprite = Sprite.Create(texture, new Rect(0, 0, width, height), Vector2.zero);
GetComponent<SpriteRenderer>().sprite = sprite;
Color pixelColour = new Color(1, 1, 1, 1);
for (int x = 0; x < texture.width; x++) {
for (int y = 0; y < texture.height; y++) {
texture.SetPixel(x, y, pixelColour);
}
}
texture.Apply();
}
}
:
But for some reason this is what i get:
:
:
It should be filling entire screen with each corner matching up....
capture.png
(11.6 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612194127im_/https://answers.unity.com/themes/thub/images/avi.jpg)