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 mmangual_83 · Jul 30, 2014 at 05:34 PM · c#ngui

Need help working with NGUI progress bar

I am new to NGUI and now I find myself tinkering with the progress bar: a tool I want to implement in my game.

Before, I had a regular GUI window with two labels being drawn and that was how I displayed the user's progress.

This is what it looks like:

alt text

This is what I have in code. This class handles drawing the numbers onto the screen.

 using UnityEngine;
 using System.Collections;
 using Simulation;
 using System;
 
 public class ProgressTracker : MonoBehaviour
 {
     //Singleton
     public static ProgressTracker objectInstance;
 
     //GUI Elements
     public Rect GUIRectWindow, // the GUI background for the progress tracker
                 r_msg; //a message that will display "Gestures Left"
 
     private int _TotalTaskCount = 0;
 
     void Start()
     {
         if (objectInstance == null)
         {
             objectInstance = this;//keep using the same game object
             DontDestroyOnLoad(gameObject);
         }
         else
             Destroy(this);
 
         _TotalTaskCount = TaskCollection._tasksSum;
     }
 
     // Update is called once per frame
     void OnGUI()
     {
         if (!Application.isLoadingLevel)
         {
             GUI.Box(GUIRectWindow, "");
             drawLabel(r_msg, "Progress: " + GameManager.Get.CompletedTasks.ToString() + " / " + _TotalTaskCount);
             
         }
     }
     public void ProgressTracking()
     {
         //TODO: NGUI Progress bar logic???
     }
     void drawLabel(Rect rect, String labelname)
     {
         GUI.Label(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), labelname);
     }
 }


What I want to know is what are the steps required to transfer the information I have in my current progress tracker into NGUI's progress bar? Many thanks in advance!

progress.png (973 B)
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

2 Replies

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

Answer by smallbit · Jul 31, 2014 at 01:21 AM

In NGUI is way easier to do that (I assume u know how to create NGUI elements)

  1. Compose your progres bar of NGUI sprites( like put some background etc.)

  2. On the top put another sprite (lond ractangle) which will be your actual progress bar, in Uisprite settings change "sprite type to filled", and "fill dir" horizontal. Play with fill amount slider to see how it will look.

  3. On the top of that (higher depths) add a UIlabel.

  4. In Any script define two references like in code below, and drag previously created sprite and label to these slots.

    public UISprite myBar; public UILabel myLabel;

  5. In your code update values of these items:

    void UpdateBar() {

    myBar.fillAmount = curentExp/(float)expRequiredForNextLevel; myLabel.text = currentExp + "/" + expRequiredForNextLevel;

and voila, you can update them in Update, but i normally always try to only update them when their value changes.

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
avatar image
1

Answer by Saad_Khawaja · Jul 31, 2014 at 01:19 AM

NGUI Progress Bar uses a UISlider component - the variable that you need to change is "value" which ranges from 0 to 1f.

So you need to update your drawLabel function and divide your value which should range from 1-15 by the total (15) to get it within the range of 0-1f. Then assign that value to the NGUI Progress bar and it will be updated instantly.

     void drawLabel(Rect rect, int value)
     {
 float percentageValue = value/15f;
        GameObject.Find("ProgressBarName").GetComponent<UISlider>().value = percentageValue;
 
     }
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

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

24 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

Related Questions

MissingReferenceException: The object of type 'ConversationController' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. 1 Answer

Distribute terrain in zones 3 Answers

Object Reference Not Set As Instance error from ngui code 1 Answer

Multiple Cars not working 1 Answer

panel hide and show in NGUI 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