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 /
This question was closed Nov 10, 2013 at 09:13 PM by MadJohny for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by MadJohny · Nov 10, 2013 at 05:00 PM · c#guibarpower

Golf Gamelike power bar

Hi, I have done a power system and it's working fine I think, but I don't know how to make the GUI part, I want the bart to be kinda like in this game: http://www.ebark.se/UNITY/golf_1.html (different visuals though, but vertical, also the bar goes up and down instead of just stopping at the top), can anyone help me with this GUI task?I use c# btw

Thanks in advace

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

  • Sort: 
avatar image
0

Answer by RyanZimmerman87 · Nov 10, 2013 at 07:50 PM

I'll try to do a quick example for you but you'll need to customize the timings with the math to perfect this. This is example for general functionality:

     //style to easily format your GUI displays
         
                         public GUIStyle swingStyle;
                         
             public Texture2D emptyBarTexture;
             //texture to display at swings peak
             public Texture2D fullBarTexture;
             public Texture2D currentSwingPowerTexture;
                         
     //vector 2 size and position for your bar
     
     Vector2 vectorSwingPosition = new Vector2 (200, 240);
     
     Vector2 vectorSwingSize = new Vector2 (50, 300); 
                         
     //floats for your bar's display %
     float currentSwingPower;
     float maxSwingPower;
     float swingCompletePower;
                         
                         //the float for the percentage currentSwingPower/maxSwingPower
                         float swingDisplayFloat;
                         
                         //swing currently active bool
                         bool swingActiveBool;
                         //bool for if power bar going up or down
                         bool swingUpBool;
                         
                         void OnGUI()
                         {
     
                         if (swingActiveBool == false)
                         {
                         //empty container group
                         GUI.BeginGroup(new Rect (vectorSwingPosition.x, vectorSwingPosition.y, vectorSwingSize.x, vectorSwingSize.y));
                         
             //empty container texture
             GUI.Box(new Rect(0, 0, objectiveSize.x, objectiveSize.y), emptyBarTexture, swingStyle);
                         return;
                         }
                         
             else if (SwingActiveBool == true)
             {
 //empty box for background
             GUI.BeginGroup(new Rect (vectorSwingPosition.x, vectorSwingPosition.y, vectorSwingSize.x, vectorSwingSize.y));
                                             GUI.Box(new Rect(0, 0, objectiveSize.x, objectiveSize.y), emptyBarTexture, swingStyle);
                                  
             
                 
             //calculate how big the power bar will be
             GUI.BeginGroup(new Rect (0, (vectorSwingSize.y - (vectorSwingSize.y * swingDisplayFloat)), vectorSwingSize.x, vectorSwingSize.y * swingDisplayFloat));
                 
             //draw power bar texture
             GUI.Box(new Rect (0, -vectorSwingSize.y + (vectorSwingSize.y * swingDisplayFloat), vectorSwingSize.x, vectorSwingSize.y), currentSwingPowerTexture, swingStyle);
                                                                 GUI.EndGroup();
             GUI.EndGroup ();
             }
                         
             }
                         
                         
                         void Update()
                         {
                         //if your key press to start swing
                         //substitute your desired button
                         if (Input.GetButtonDown ("Fire1"))
                         {
                         
                         if (swingActiveBool == false)
                         {
                         swingActiveBool = true;
                         swingUpBool = true;
                         }
                         
                         else if (swingActiveBool == true)
                         {
                         if (swingUpBool == true)
                         {
                         maxSwingPower = currentSwingPower;
                         swingUpBool = false;
                         }
                         
                         else if (swingUpBool == false)
                         {
                         swingCompletePower = currentSwingPower;
                         swingActiveBool = false;
                         
                         }
                         
                         //calculate bar Percentage
                         swingDisplayFloat = currentSwingPower/maxSwingPower;
                         }
                         
                         }
                         
                         
                         //do your bar calculations at Fixed TimeStep
                         void FixedUpdate()
                         {
                         if (swingActiveBool == false)
                         {
                         return;
                         }
                         
                         else if (swingActiveBool == true)
                         {
                         if (swingUpBool == true)
                         {
                         ++currentSwingPower;
                         }
                         
                         else if (swingUpBool == false)
                         {
                         --currentSwingPower;
                         }
                         }
                         }
     
 

Ugh sorry for the formatting problems don't really have time to clean all that up. I really seem to make a mess of my code examples when they are big not sure if the system just hard to use or something about the way I format my code throws it off big time :(

Well that's the basic example of how to draw a power bar within a container bar based on what percentage of the bar should be displayed.

This example is missing quite a bit of stuff you'll need for the final version but at least you'll be able to get started more easily now.

Things you'll still need just in case it's not clear what this example includes:

1) Way to display the max power texture for the peak of swing

2) Way to display the finish power texture for end of swing

3) Way to make the bar persist a little bit after the swing finishes instead of instantly clearing the bar.

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

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullreferenceException, BeginScrollView style change 3 Answers

Scaled GUI 1 Answer

Create a Button Scrollview 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