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.

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:

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);

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

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. Redirect to... title of new-page