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 Entyro · Apr 15, 2014 at 11:11 AM · guiflashlight

Gui placement help

Hi!

I have a problem with my flashlight script for placing an energy bar by using GUI.DrawTexture. In Unity it looks good (check image below).

Note! The flashlight bar is the green one! The yellow one is a stamina bar!

alt text

But when I'm playing the game after "Building" it it looks different for different resolutions (check images below).

alt text

The script

 var BatteryTexture : Texture;
 var border : Texture;
 var lightSource : Light; //Connect the light source in the Inspector
 var FlashlightSound : AudioClip;
 static var energy : float = 0.0; //The energy amount of the flashlight
 private static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
 var drainSpeed : float = 0.4; //The speed that the energy is drained
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
 
     if (Input.GetKeyDown(KeyCode.F)) 
     {
         audio.PlayOneShot(FlashlightSound);
     }
     
 }
 
 
 
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () 
 {
     turnedOn=!turnedOn;
     if (turnedOn && energy>0) 
     {
        TurnOnAndDrainEnergy();
     } 
     else 
     {
        lightSource.enabled = false;
     }
     
 }
 
 
 function OnGUI ()
 {
     GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
     GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );
     
     
 }
 
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () 
 {
     lightSource.enabled = true;
     
     while (turnedOn && energy>0) 
     {
        energy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightSource.enabled = false;
 }
 
 //This is called from outside the script to alter the amount of energy
 static function AlterEnergy (amount : int) 
 {
     energy = Mathf.Clamp(energy+amount, 0, 100);
     
 }

Do any of you guys know how to fix this?

pic1.png (149.6 kB)
pic2.png (316.6 kB)
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
1
Best Answer

Answer by fafase · Apr 15, 2014 at 11:32 AM

Your dimensions for the rects are hardcoded:

 GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
 GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );

Your border texture is 405 pixels regardless the screen dimensions.

To fix it use the screen dimensions to set your rect:

 var width = Screen.width / ratioWidth;
 var height = Screen.heighr / ratioHeight;
 var xPos = Screen.width / marginRatio;
 var yPos = Screen.height / heightRatio;
 
 var rect :Rect = Rect(xPos, yPos, width, height);
Comment
Add comment · Show 2 · 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 Entyro · Apr 15, 2014 at 11:41 AM 0
Share

So I just add that code at the top of the script and leave the gui code as it is?

I get some errors when i do that, "$$anonymous$$ identifier: ratioWidth", "$$anonymous$$ identifier: ratioHeight" etc...

avatar image fafase · Apr 15, 2014 at 12:29 PM 0
Share

Well, you need to declare them, probably as public so that you can tweak them and find the most suitable.

You could leave this code in the OnGUI, until you find satisfactory values. Then you move it all to the Start.

One thing though, if you do it in the Start and you change the size of the window, this will not recalculate, keeping it in the OnGUI would make the calculation on each run so if the screen size changes so does the values.

But the best is to use an event like checking in an Update if the width and height have changed and trigger an event for that.

It is all up to what you are after

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

20 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

Related Questions

Multiple Cars not working 1 Answer

GUI problem 1 Answer

Full Screen Get smaller when i actived it 1 Answer

Pause Menu background problem 0 Answers

Pause Menu 5 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