Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 user-860 (yahoo) · Feb 04, 2010 at 03:36 AM · guiresolutionscreencenter

Rendering GUI.Buttons in the middle of the screen

Hey I am having some trouble with my GUI. In my main menu they buttons render in the middle of the screen, but if I switch to a widescreen or change the resolution the buttons move and sometimes you can't even see them. Does anyone know how to fix this?

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Wolfram · Feb 25, 2010 at 04:49 PM

Depends on how you placed your buttons. With GUIText and GUITexture, the "Pixel Offset"/"Pixel Inset" parameters are problematic, since they are pixel based, and therefore, placement of your GUI elements will depend on your screen resolution.

I wrote a little wrapper script, which normalizes screen positions to -0.5..0.5, and also takes care of the aspect ratio, and translates these values to pixel offsets depending on your screen resolution. This way, you can place your GUI elements consistently for any screen size.

In essence, you need to do something like (C# example for GUIText):

public float X; public float Y;

 ...

 float Xcoord=X*Screen.width;
 float Ycoord=Y*Screen.height;

 // do we have a GUIText?
 GUIText guiText=GetComponent<GUIText>();
 if(guiText){
     // translate to normalized position
     guiText.pixelOffset=new Vector2(Xcoord,Ycoord);
 }

Comment
Add comment · Show 3 · 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
avatar image agentsmith · Jul 03, 2010 at 09:11 AM 0
Share

Wow totally awesome!! Thank you Wolfram, it worked beautifully!!!

avatar image Juri · Oct 22, 2010 at 11:24 AM 0
Share

where is this script supposed to be implemented???

avatar image Wolfram · Oct 25, 2010 at 01:05 PM 0
Share

You need to set these only at initialisation time, so Awake() or Start() will do.

avatar image
2

Answer by BerggreenDK · Aug 04, 2011 at 11:04 PM

I usually just grab the width and height from the Screen.width and Screen.height. This gives me the current resolution. You can do this in the top of your OnGUI even or just store them in variables that you update when you switch resolution.

To center elements, you'll need to know the width and height of your element to center.

Formular is rather simple:

 int xpos = (Screen.width)-(this_element.width)/2;
 int ypos = (Screen.height)-(this_element.height)/2;


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
avatar image
1

Answer by Sebas · Feb 04, 2010 at 04:44 AM

With Screen.width you can access the width of your screen in pixels. Divide that by half and you have the exact middle of your screen. Position the buttons at the appropriate spot by taking a certain percentage of the screen width (e.g. placing them around 45% of your screen width). That should keep them in the middle no matter what your resolution is.

Comment
Add comment · Show 1 · 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
avatar image landon912 · Aug 31, 2012 at 01:34 AM 0
Share

That will not in fact, in Unity the textures are positioned by there lower left corner so they will not be centered.

avatar image
0

Answer by KFVex · Aug 29, 2012 at 09:00 PM

In C# Script:

 if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 50)
 {
     //Enter Code here
 }
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
avatar image
0

Answer by hendrakid · Dec 25, 2014 at 02:17 PM

try this one ! for square button

 if (GUI.Button (new Rect ((Screen.width/2)-(Screen.width/4), (Screen.height/2)-(Screen.width/4), Screen.width/2, (Screen.height-(Screen.height-Screen.width))/2),"",Question)) {
             
             Debug.Log("Center");        
         }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How Do I Center A GUI Label? 5 Answers

Image overlay in center of screen 2 Answers

Android Screen Resolution 1 Answer

Auto scaling GUI 3 Answers

GUI.DrawTexture Center on screen 1 Answer


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