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 davooo0 · Nov 02, 2013 at 01:59 AM · resolution settings

How do i scale health bar with resolution

How can i edit this So this scales with the resolution

     static var playerHealth : int = 100;
 
     var  HealthyTexture : Texture2D;
     var  UnHealthyTexture : Texture2D;
     var  CurrentTexture : Texture2D;
 
      GUI.Label(Rect(Screen.width/ 1.15, Screen.height/1.05, 50, 20),"Health:");
     GUI.DrawTexture(Rect(Screen.width/ 1.12, Screen.height/1.05, (200 * (playerHealth / HealthMax)), 20),CurrentTexture);
     GUI.Box(Rect(Screen.width/ 1.156, Screen.height/1.13, 255, 100)," ");
 

This was written for a 1920x1080 display

Comment
Add comment · Show 1
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 davooo0 · Nov 04, 2013 at 09:36 AM 0
Share

doing that messed up the positions and wont go back no matter what i do

1 Reply

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

Answer by Fornoreason1000 · Nov 02, 2013 at 03:26 AM

here is what i would do... setup a native resolution...myDisplay is 1080p but most games will run at 720p, so

Native = 1280x720

lets say you bump it up to 1080p which is fully supported the ratio between them is in this case Yration and Xratio are the same, but this will not alway be the case(e.g640x480). so we give it two vars.

Xratio = 1920 / Native.x Yratio = 1080 / Native.y

you multiply Xratio to your width and x coordinate and the latter for Yratio.

  public class MyGUISystem : MonoBehaviour
     {
         Rect LabelRect;
         Rect TextureRect;
         Rect BoxRect;
         Texture2D CurrentTexture;
 
         int HealthMax = 100;
         static int playerHealth = 100;
 
         /// <summary>
         /// Use this fopr Initialization
         /// </summary>
         public void Start()
         {
             UpdateRects();
 
         }
 
         /// <summary>
         /// Call this when you chagne resoltution
         /// </summary>
         public void UpdateRects()
         {
 
             LabelRect = new Rect(Screen.width / 1.15f * (Screen.width / 1920.0),
                 Screen.height / 1.05f * (Screen.height / 1080.0),
                 50.0 * (Screen.width / 1920.0),
                 20.0 * (Screen.height / 1080.0));
 
             TextureRect = new Rect(Screen.width / 1.12f * (Screen.width / 1920.0),
               Screen.height / 1.05f * (Screen.height / 1080.0),
               (200.0 * (MyGUISystem.playerHealth / HealthMax) * (Screen.width / 1920)),
               20.0 * (Screen.height / 1080.0));
 
             BoxRect = new Rect(Screen.width/ 1.156f  * (Screen.width / 1920.0),
                 Screen.height/1.13f * (Screen.height / 1080.0),
                 255.0  * (Screen.width / 1920.0),
                 100.0 * (Screen.height / 1080.0));
         }
 
        public void OnGUI() {
          GUI.Label(LabelRect, "Health");
         GUI.DrawTexture(TextureRect, CurrentTexture);
         GUI.Box(BoxRect, " ");
         }
 
     }


Comment
Add comment · Show 19 · 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 davooo0 · Nov 02, 2013 at 09:25 AM 0
Share

thanks for the script will go over it

also do i just put the script on my player or do i merge it into my current script

avatar image Fornoreason1000 · Nov 02, 2013 at 10:46 AM 0
Share

its the same script you posted... but in C# and it scales with your Resolution

avatar image davooo0 · Nov 02, 2013 at 11:29 PM 0
Share

O$$anonymous$$ it did not want to work in C# so i converted it to JavaScript thanks

avatar image davooo0 · Nov 02, 2013 at 11:32 PM 0
Share

hmm it just shows up as a black square on the screen

avatar image Fornoreason1000 · Nov 03, 2013 at 05:11 AM 0
Share

oh yeah... make sure each value is a float not an int... you need a decimal or float because you a multiplying values that can be less than 1, if its an int, it will multiply by zero...

 var LabelRect : Rect;
  var TextureRect : Rect;
 var BoxRect : Rect;
 var CurrentTexture : Texture2D;
  
     var Health$$anonymous$$ax : int = 100;
     static var playerHealth : int  = 100;
  
     /// <summary>
     /// Use this fopr Initialization
     /// </summary>
      function Start()
     {
         UpdateRects();
  
     }
  
     /// <summary>
     /// Call this when you chagne resoltution
     /// </summary>
      function UpdateRects()
     {
  
         LabelRect =  Rect((Screen.width / 1.15) * (Screen.width / 1920.0),
             (Screen.height / 1.05) * (Screen.height / 1080.0),
             50.0 * (Screen.width / 1920.0),
             20.0 * (Screen.height / 1080.0));
  
         TextureRect =  Rect(Screen.width / 1.12 * (Screen.width / 1920.0),
           Screen.height / 1.05 * (Screen.height / 1080.0),
           (200.0 * ($$anonymous$$yGUISystem.playerHealth / Health$$anonymous$$ax) * (Screen.width / 1920.0)),
           20.1 * (Screen.height / 1080.1));
  
         BoxRect =  Rect((Screen.width/ 1.156)  * (Screen.width / 1920.0),
             Screen.height/1.13 * (Screen.height / 1080.0),
             255.0  * (Screen.width / 1920.0),
             100.0 * (Screen.height / 1080.0));
     }
  
      function OnGUI() {
         GUI.Label(LabelRect, "Health");
         GUI.DrawTexture(TextureRect, CurrentTexture);
         GUI.Box(BoxRect, " ");
     }
  


you wil also need to adjust the font size of that Label, or it will clipo and cut over itself, as the font itself isnt adjsut... you can do this in the same manner as i did with the Rects

Show more comments

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

16 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

Related Questions

Sprites proportions for a HD 2D game 0 Answers

help with an aspect ratio screen 1 Answer

Setting game as full screen, but opens in window between two monitors 1 Answer

Setting a game size 0 Answers

"Couldn't switch to requested monitor resolution" 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