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 Blybek · Jan 21, 2015 at 10:18 AM · guiresolutionscreenadjustcross

Adjusting GUI

Hello. I have made some GUI graphics and now i want to add them to the game.The problem is when i change the resoultion, the graphics are moving up or under the screen.How can i make the GUI to adjust to the resoultion of computer? Heres the code i use to it:

 using UnityEngine;
 using System.Collections;
 
 public class Health : MonoBehaviour {
 
     private float maxHealth = 100;
     private float currentHealth = 100;
     private float maxArmour = 100;
     private float currentArmour = 100;
     public Texture2D cross;

     
     void OnGUI() 
     {
         GUI.Label (new Rect (65,Screen.height - 25,100,50), "HP " + currentHealth);
         GUI.Label (new Rect (20,Screen.height - 30,Screen.width/10,Screen.height/12), cross);
         GUI.Label (new Rect (200,Screen.height - 25,100,50), "Armor " + currentArmour);
     }     
     
     void takeHit(float dmg) 
     {
         if(currentArmour > 0) {
             currentArmour -= dmg;
             if(currentArmour < 0) {
                 currentHealth += currentArmour;
                 currentArmour = 0;
             }
         } else {
             currentHealth -= dmg;
         }
         
         currentArmour = Mathf.Clamp(currentArmour, 0, maxArmour);
         currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
     }
 }
 

This is the line that makes "Cross"(graphic) show on the screen

 GUI.Label (new Rect (20,Screen.height - 30,Screen.width/10,Screen.height/12), cross);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ricardo_arango · Jan 21, 2015 at 11:22 AM

You are positioning the cross texture using a fixed width and height, regardless of the screen size:

 new Rect (20,Screen.height - 30

but you are setting the size using a width and height relative to the screen size:

 Screen.width/10,Screen.height/12

That means that it's possible for the object to be outside of the screen bounds.

What you need to do is to position the object also using values relative to the object's size (which you are calculating using the Screen size). For example this code should place the cross in the corner of the screen:

 float width = Screen.width/10;
 float height = Screen.height/12;
 GUI.Label (new Rect (height/2, Screen.height - height/2, widtdh, height), cross);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI adapting to screen resolution? 3 Answers

How to maintain high resolution custom background images for GUI elements on different screen sizes? 0 Answers

GUI texture to fit the screen at any resolution? 4 Answers

GUI and Screen Resolution 1 Answer

Best approach to define interactive screen areas regardless of it's resolution? 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