- Home /
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 :)
As long as the calculations are done within OnGUI(), the buttons should resize themselves correctly.
I know, but i wouldnt like to calculate them every OnGUI call, only when it is neccessary
What you can do then is check if the resolution of the screen has changed. Just make two variables, one for Screen.width and one for Screen.height and each frame check if they have changed, if they changed, recalculate the button properties.
Well yeah, i guess i didnt really write a clear explanation. I am doing exactly that you did write. The thing is i have like 25 Button instantiated. If i would like to resize them i have to store a reference for all of those 25 Buttons and for their rectangles. That is just simply too much code. If i could solve the resize inside of my Button class i could remove this code.
Lets say I have got a Button created with the following rect: new rect(Screen.width/10.0f,Screen.height/2.0f,Screen.width/20.0f,Screen.height/15.0f).
The problem is that inside the Button class i dont know which property they depend on (Screen.width,Screen.height, maybe both).
And the question is: despite the fact that i dont know which property they depend on is there a way that i can properly resize them so that they wont lose the previously defined ratio?
I haven't tested it yet, but I think something like this should do the trick. Just pass the Screen.width, Screen.height and the Rect when instantiating the Button.
public class Button : $$anonymous$$onoBehaviour {
int originalWidth;
int originalHeight;
Rect originalRect;
Rect currentRect;
public Button(int originalWidth, int originalHeight, Rect originalRect) {
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
this.originalRect = originalRect;
this.currentRect = this.originalRect
}
public void Resize() {
currentRect = new Rect(Screen.width/(originalWidth/originalRect.x), Screen.height/(originalHeight/originalRect.y),
Screen.width/(originalWidth/originalRect.width), Screen.height/(originalHeight/originalRect.height));
}
}
Answer by Razacx · Jul 14, 2014 at 11:17 AM
(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));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613153938im_/https://answers.unity.com/themes/thub/images/avi.jpg)