Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Mar 25, 2017 at 02:04 AM by dgalbraith3 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by dgalbraith3 · Mar 16, 2017 at 08:09 PM · uicountdownbarincrementdecrease

C# Creating a bar that decrements but can be incremented when pieces are matched

Hello I am working on a 2D match3 game and was wanting to have a bar that starts at 10 for example and decrements down but if a player matches something for it to increment. I am still learning coding, if anyone would be able to help me with this it would be greatly appreciated. I know how to reference when pieces are matched I'm more concerned about how to create a bar and have it decrease and increment.

Thanks in advance!

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

Answer by Matt1000 · Mar 16, 2017 at 08:46 PM

The way i would go to do is is with IMGUI ( Immediate Mode Graphical User Interface). IMGUI is a way to create run-time GUI through code. What I'd reccomend you is to have a script attached to your camera that has some GUI functions (I do it in the camera just as reminder where my GUI scripts are).

So, probably the best way to create this bar is to use sprites. Sprites, different from simply drawing sqares in your screen, enable you more detailed textures and can be simply changed by changing the .JPG or .PNG file. To do a bar you'd need 2 sprites, one for the empty bar (e.g: grey square) and one for the full part of the bar (e.g: purple square).

This is the first code you'll need:

 using UnityEngine;
 using System.Collections;
 
 public class IMGUIThingy : MonoBehaviour {
 
     Texture2D emptyBar;
     Texture2D fullBar;
 
     Vector2 position; //This is where you want the top-left corner to be in your screen.
 
     [Range (0,100)] //This is the range between your fullPorcentage's value can be in (min = 0 and max = 10).
     public float fullPorcentage;
 
     float barWidth;
     float barHeight;
 
     public void Awake () {
         emptyBar = Resources.Load<Texture2D> ("Empty"); //This will find your texture using the file name.
         fullBar = Resources.Load<Texture2D> ("Full");
     }
 
     //All this variaables are here because the Screen's size might change.
     public void Update () {
         position.x = Screen.width * 0.15f;
         position.y = Screen.height * 0.75f;
         //This is at the bottom of the screen
 
         barWidth = Screen.width * 0.7f;
         barHeight = Screen.height * 0.15f;
     }
 
     //Here you will put all the GUI methods you need.
     public void OnGUI () {
         GUI.DrawTexture (new Rect (position, new Vector2 (barWidth, barHeight)), emptyBar);
         GUI.DrawTexture (new Rect (position, new Vector2 (barWidth / 100 * fullPorcentage, barHeight)), fullBar);
     }
 }

When you finish this you'll see that your camera has one variable where you can select fullPorcentage's value, and as you change it the purpule part of the bar will change. If you want the bar to decrease and just to increase when something happens you just add these details:

     public float decreaseAmountPerSecond;
     public float increaseAmountWhen3InRow;
 
     public void Update () {
         //Constantly decrease
         fullPorcentage -= decreaseAmountPerSecond * Time.deltaTime;
     }
 
     //Call this method when 3-in-Row happens
     public void ThreeInRow () {
         fullPorcentage += increaseAmountWhen3InRow;
     }

Hope this helps you a lot! :D

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

133 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 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 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 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 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 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 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

Count Down Timer C# Conversion Script Not Working Help 3 Answers

Health Bar issue 0 Answers

Timer Countdown Pop Up Screen Before Scene "Starts" Please Help!! 2 Answers

How to increase +1 when countdown reachs to 0? 0 Answers

Make the health bar fill smoothly over time 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