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
-1
Question by SVGK · Feb 06, 2014 at 08:05 PM · gui-problem

GUI meter moves in the wrong direction, how do i fix this?.

i am making a fuel meter GUI for a game, but i've run into a problem, the meter fills from the top down, instead of the other way around.

 using UnityEngine;
 using System.Collections;
 
 public class FuelGauge : MonoBehaviour
 {
     public float barDisplay; //current progress
 
     public Vector2 pos = new Vector2(20,40);
     public Vector2 size = new Vector2(50,250);
 
     public Texture2D emptyTex;
     public Texture2D fullTex;
     
     void OnGUI() 
     {
         //draw the background:
         GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
         GUI.Box(new Rect(0,0, size.x, size.y), emptyTex);
         
         //draw the filled-in part:
         GUI.BeginGroup(new Rect(0,0, size.x * barDisplay, size.y));
         GUI.Box(new Rect(0,0, size.x, size.y), fullTex);
         GUI.EndGroup();
         GUI.EndGroup();
     }
     
     void Update() 
     {
         barDisplay = Fuel.currentFuel / Fuel.maxFuel;
     }
 }

i would fix this myself, but i'm not sure how to fix it to be honest, not sure what's wrong either, i think it's because unity renders rectangles and GUIs from the top right corner, but i don't know how to remedy that.

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
Best Answer

Answer by Maui-M · Feb 06, 2014 at 09:24 PM

The code below should offset the box by what is not filled in, just replace what you have at line 21-22 from above.

 float bar = size.x * barDisplay;
 GUI.BeginGroup(new Rect(0,0, size.x, size.y));
 GUI.Box(new Rect((size.x-bar,0, size.x * barDisplay, size.y), fullTex);
Comment
Add comment · Show 7 · 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 SVGK · Feb 06, 2014 at 10:00 PM 0
Share

hrm, this didn't quite work, the fuel meter now just won't increase at all, just stays stuck on empty.

avatar image Maui-M · Feb 06, 2014 at 11:54 PM 0
Share

Updated the code, try that.

avatar image SVGK · Feb 07, 2014 at 09:52 AM 0
Share

hmm, i got a few errors because of a typo, i fixed that, but the bar does something really strange, ins$$anonymous$$d of just going down like expected, the "full" texture shrinks in size.

 using UnityEngine;
 using System.Collections;
 
 public class FuelGauge : $$anonymous$$onoBehaviour
 {
     public float barDisplay; //current progress
     
     public Vector2 pos = new Vector2(20,40);
     public Vector2 size = new Vector2(50,250);
     
     public Texture2D emptyTex;
     public Texture2D fullTex;
     
     void OnGUI()
     {
         //draw the background:
         GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
         GUI.Box(new Rect(0,0, size.x, size.y), emptyTex);
         
         //draw the filled-in part:
         float bar = size.x * barDisplay;
         GUI.BeginGroup(new Rect(0,0, size.x, size.y));
         GUI.Box(new Rect(size.x-bar,0, size.x * barDisplay, size.y), fullTex);
         GUI.EndGroup();
         GUI.EndGroup();
     }
     
     void Update()
     {
         barDisplay = Ball$$anonymous$$ain.currentFuel / Ball$$anonymous$$ain.maxFuel;
     }
 }

what i'm trying to achieve is like a normal health meter in a game, only ins$$anonymous$$d of being horizontal and filling from the left, it's vertical and fills up from the bottom up.

avatar image Maui-M · Feb 07, 2014 at 05:21 PM 0
Share

Your original code was set up modifying the X values so it just seemed like that was the way your camera was set up so X was up/down. If it isn't then use the code as an example and change the Y values ins$$anonymous$$d of X.

avatar image SVGK · Feb 07, 2014 at 06:26 PM 0
Share

i'm sorry if i come off as stupid, i don't get what y vales to change, from reading the code and thinking about it, my only assumptions as for what to change are.

 float bar = size.y * barDisplay;
 GUI.BeginGroup(new Rect(0,0, size.x, size.y));
 GUI.Box(new Rect(size.x-bar,0, size.x * barDisplay, size.y), fullTex);
     
     
 or
     
     
  float bar = size.y * barDisplay;
 GUI.BeginGroup(new Rect(0,0, size.x, size.y));
 GUI.Box(new Rect(size.y-bar,0, size.x * barDisplay, size.y), fullTex);
     
     
 or
     
     
  float bar = size.x * barDisplay;
 GUI.BeginGroup(new Rect(0,0, size.x, size.y));
 GUI.Box(new Rect(size.y-bar,0, size.x * barDisplay, size.y), fullTex);
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

19 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

Related Questions

4.3 GUI 2D Question 1 Answer

Intro, Text scrolling wrong direction (Down not Up) 2 Answers

Can't see GUI layers or sprites 3 Answers

CountDown Camera problem 0 Answers

Unity 3d GUI text problem PLEASE HELP :| 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