Resizing 'Image'

Hello

I am a newbie.

I created a Image in a Canvas. I then try to resize the image by adding a script to it. The script is as follows:

myImage.Resize(4, 4);
myImage.Apply();

However the image does not resize. It stays the same size as before.

Any idea what could I be missing?

Thanks.

The Image component does not have a Resize method. You’re calling Resize on the Texture of the image. That’s an obsolete method that changes the resolution of the image.

In order to change the size or position of anything on a Canvas, you want to grab the RectTransform on the same GameObject, and change it’s values. For size, you’d want to change the .sizeDelta value of the RectTransform.

2 Likes

I tried this:

RectTransform rt = image.GetComponent (typeof (RectTransform)) as RectTransform;
rt.sizeDelta = new Vector2 (100, 100);

Still doesn’t work.

Ok. Got it working. Thank You so much.

RectTransform rt = brainImage.GetComponent (typeof (RectTransform)) as RectTransform;
    rt.sizeDelta = new Vector2 (600, 300);
1 Like

Great!

Where are you finding these ancient-ass ways of doing things? GetComponent(typeof(T)) as T; hasn’t been the standard since idk 2015? It works, I guess, but I’d suggest finding a tutorial that wasn’t written while the WiiU was still relevant.

6 Likes