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 Nercoe · Nov 07, 2012 at 07:39 PM · positionresolutionfixed

Fixed position of GUI content (same for every resolution).

Hi guys, just wondering what the best way to do this is. I want a button on my screen located at a set point. I want the button to appear here despite the screen resolution. Can anyone tell me the best way to do this? Thank you in advance. So far I can print a texture to fit the screen size despite the resolution with:

     function OnGUI()
         {
             GUI.DrawTexture(Rect(0.0f, 0.0f, Screen.width, Screen.height), box1Texture);

         }

But now I need to know how to do it with objects such as buttons and display them at the same area every time. Thanks for any help :)

Please note: I am not asking for scripts

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FakeBerenger · Nov 08, 2012 at 03:33 AM

Use fraction of the Screen's size for you're rect. Like : Rect(Screen.width .1f, Screen.height .1f, 100f, 100f)

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 JMC17 · Oct 07, 2013 at 10:05 AM

I understand this is an old thread, hopefully it can still help someone.

If using GUI.Button, this is the way:

 void OnGUI(){
 GUI.backgroundColor = new Color(0, 0, 0, 0);
 if (GUI.Button (new Rect(Screen.width-80,(Screen.height*0)+10,64,32),ButtonTexture)){ButtonPressed();};
 }
     
 void ButtonPressed(){
 Debug.Log("Button was pressed");
 }

If not, then you have to create an empty gameObject and fill it with a guiTexture. Set the transform scale to 0,0,1 ; Set the pixelinset x,y to 0 and width height to those of your image then use transform position as a percentage of the screen, by setting the transform position between 0 and 1 (0,0 bottom left and 1,1 top right) Then create a Rect based on your image's size and position and check for touches within it using Touch and Input.

For the GUI to be fixed in size and position it is better to use a pixel offset than a screen percentage. For screen percentage you would do: Screen.width*0.9f (and get your image positioned at 90% of the screen width)

But for pixel offset you would have to do:

 float sw=Screen.width //(Converting int to float)
 float rsw=sw-30 //(30 pixels away from the right side(removing 30 pixels from Screen.width))
 float rrsw=rsw/sw //(Dividing the position to get the final percentage of the screen(Converting the modified position to be a value between 0 and 1))
 
 transform.position=new Vector3(rrsw,Screen.height*0.5f,1);

You could also do it for the height too, in the example above I just left it with a screen percentage.

I would leave the complete code but I have none on hand, sorry!

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 Zac-Oberg · Mar 11, 2020 at 02:38 AM

Inevitably somebody is going to stumble upon this thread to make a button that sticks to the corner of the screen:

  1. Create a new script

  2. Copy and paste this entire snippet

  3. Attach it to an empty gameobject

  4. Profit!

      [ExecuteInEditMode]
     public class fixedButton : MonoBehaviour
     {
         public float width=100;
         public float height=40;
         public float updown=3;
         public float leftright=25;
        void OnGUI()
         {
         if (GUI.Button (new Rect(Screen.width-(leftright*5),(Screen.height*(updown/50)),width,height), "Button text!"))
             {
             ButtonPressed();
             };
         }
          
         void ButtonPressed()
         {
             Debug.Log("Button was pressed");
         }
     }
    
    
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

12 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

Related Questions

Windowed game not centered 0 Answers

positioning a gameobject 10 persent of screen 3 Answers

Best way to move elements around screen [uGUI] 0 Answers

GUI Label Size and Position Changes With Resolution 2 Answers

Pause Menu Screen Resolution 0 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