Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dsada · Jul 13, 2014 at 10:04 AM · guiresize

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Razacx · Jul 13, 2014 at 10:49 AM 0
Share

As long as the calculations are done within OnGUI(), the buttons should resize themselves correctly.

avatar image dsada · Jul 13, 2014 at 11:05 AM 0
Share

I know, but i wouldnt like to calculate them every OnGUI call, only when it is neccessary

avatar image Razacx · Jul 13, 2014 at 01:02 PM 0
Share

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.

avatar image dsada · Jul 13, 2014 at 01:53 PM 0
Share

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?

avatar image Razacx · Jul 13, 2014 at 08:57 PM 1
Share

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));
                                                 
     
     }
 
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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));
 
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help Changing GUI Text size 0 Answers

Can you update the application while resizing the window? 0 Answers

New 4.6 UI question: How do you resize a panel relative to the screen but remain height/width constrained? 5 Answers

How can I resize an image with drag-and-drop? 1 Answer

What is the best way to resize textures on the GUI? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges