- Home /
Set the UI Image to fit Screen Size
I want to scale or set the height,width to fit screen size. I can do it by inspector using anchor preset,Same thing I want to do it by script.
My Canvas setting is Renderer Mode :- Screen Space - Camera , UI Scale Mode :- Scale with Screen Size.
Answer by Mukabr · Sep 30, 2016 at 04:52 PM
On RectTransform inspector UI, press on that "middle/center" graph and than, while holding Alt key, press on "Stretch/Stretch"
Using code, you can control the anchors with:
http://docs.unity3d.com/ScriptReference/RectTransform-anchorMin.html http://docs.unity3d.com/ScriptReference/RectTransform-anchorMax.html http://docs.unity3d.com/ScriptReference/RectTransform-pivot.html
public RectTransform panelRectTransform;
// Something like this.
void Start()
{
panelRectTransform.anchorMin = new Vector2(1, 0);
panelRectTransform.anchorMax = new Vector2(0, 1);
panelRectTransform.pivot = new Vector2(0.5f, 0.5f);
}
If you mean the Image got distorted, all you have to do is check the Preserv Aspect box.
Do note that your canvas' UI scale mode needs to be set to "Scale With Screen Size" and Screen Match Mode needs to be set to "Shrink" for this to work.
Answer by Muchmu · Jan 14, 2021 at 06:04 PM
solved ( i think ) it worked for me at least but i was working with a render texture but i had streching problems and this solved it so it should work for other images ( I did use a raw image on the canvas though so that might have something to do with it )
You need to
that worked for me
Answer by SaurabhStudio · Sep 30, 2016 at 04:54 PM
Set height,width to the size of canvas height,width. And set anchor point to the corner of canvas.
And for more details check this. https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
Your answer
Follow this Question
Related Questions
Need Mask Shader . 0 Answers
How to have TextMesh Pro text appear above image? 0 Answers
what will happen if i use SpriteRenderer component in a Canvas Instead of Image component ? 0 Answers
Rare players have serious issues with disappearing or distorted images and sprites 0 Answers
Convert UI element to world position 1 Answer