- Home /
How do I set a sprite to fill the entire screen in UI?
I've got myself in a little bit of a pickle. I have a sprite under a canvas but ALT + SHIFT + clicking the scale in bottom right of rect transform doesn't scale it to full screen for some weird weird reason. It works for images, but not sprites. I need to use a sprite because I'm using the sprite in conjunction with some sprite masks to create a shadow effect. Unfortunately, images don't work with sprite masks, only sprites do.
I believe I can scale sprites in code, and I think I remember doing it before, but it just seems silly that my sprite won't scale to the screen size under a canvas the same way an image will. Perhaps image is UI compatible and sprite is not, and I didn't realize that? I'm not sure.
When I say sprite vs image, I mean applying the sprite renderer vs the image components. Thanks in advance if you can help. I will probably have to end up stretching my sprite in code :(
Answer by harperrhett · May 24, 2020 at 03:17 PM
I ended up using this script, I can't figure out how to snap it to the edges but oh well...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class FitScreen : MonoBehaviour {
// Initialize
private int screenWidth;
private int screenHeight;
private Vector2 screenSize;
// Update is called once per frame
private void LateUpdate() {
// Update screen width and height
screenWidth = Screen.width;
screenHeight = Screen.height;
screenSize = Camera.main.ScreenToWorldPoint(new Vector2(screenWidth * 2, screenHeight * 2));
// Stretchy
transform.localScale = screenSize;
}
}
Your answer
Follow this Question
Related Questions
Directly refrencing a 9-spliced Sprite 0 Answers
Fixing Sprites in Unity? 0 Answers
Panel UI Organisation 0 Answers
how can i get the clicked sprite which is placed on UI canvas? 1 Answer
how can i access multiple sprites using a single variable? 1 Answer