Resizing own GUI elements

Hi everyone!

I’ve got a question that i’m almost sure there is a solution but i cant figure that out.

Got my own Button class that i can instantiate with a rect and a texture something like

float buttonSize = Screen.width/20.0f; 
Rect rectangle = new Rect(Screen.width/5.0f,Screen.height/4.0f,buttonSize*2,buttonSize);
Button button = new Button(rectangle,texture);

If the screen size changes i would like to resize them. Now i do it like count the rectangle again with the new screen properties and then call a Resize function that changes the rectangle.

I wouldn’t like to keep the reference for all of the buttons and rectangles because there are lots of them but if i dont keep them I cant resize them.

I thought of a solution that when i create the button i compare the x coordinate and the width with the Screen.width and also compare the y coordinate and the height with the Screen.height. But as the previous examples shows there are situations when the height does not depend on the Screen.height and if the aspect ratio changes for example the game is 16:9 in the webplayer and the player puts it on fullscreen on a 4:3 monitor it wont be true that the button’s width is two times its height.

So my question is that is there some math trick for the buttons to be able to “resize themselves?” I mean something so i dont have to remember the rectangle because i can deal with the resize inside of the Button class?

Hope it is understandable and thanks for your time if you are willing to help :slight_smile:

(continuing from the comments)

Well, for that you’d have to change the resize method a bit I think. But I don’t think it’s impossible at all :p.

If you need to use both variants of the resize method, you can always pass a boolean when instantiating the class. Then put an if statement in the resize method to determine which Rect to create.

public void Resize() {

        currentRect = new Rect(Screen.width/(originalWidth/originalRect.x), Screen.height/(originalHeight/originalRect.y), 
                                                Screen.width/(originalWidth/originalRect.width), Screen.width/(originalWidth/originalRect.height));