Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Progress bar Help!

Discussion in 'Immediate Mode GUI (IMGUI)' started by Mercuryman, Oct 22, 2014.

  1. Mercuryman

    Mercuryman

    Joined:
    Oct 22, 2014
    Posts:
    1
    Hi guys,

    I have been making a script for a progress bar and it works fine other than it starts in the centre then pushes out left and right. I want the bar to start left and move right till it fills then let the button spawn and be click able. any help would be great.

    My script:

    #pragma strict

    public var player: Player;

    private var currentProgress:double = 0;
    private var caseValueM:double = 1.50;
    private var caseValueF:double = 1.50;

    public var solveButtonActive:boolean = false;
    public var solveButton:GameObject;
    public static var solveButtonPressed:boolean = false;

    function Start () {

    }

    function Update ()

    {
    transform.localScale = Vector3(currentProgress/200,0,0.1);
    if (currentProgress == 100 && !solveButtonActive)
    {
    Instantiate(solveButton);
    solveButtonActive = true;
    }
    if (solveButtonPressed)
    {
    solveButtonPressed = false;
    currentProgress = 0;
    solveButtonActive = false;
    player.updateMoney(caseValueM);
    player.updateFame(caseValueF);
    }
    }

    public function progress(progression:double)
    {
    if (currentProgress < 100){
    currentProgress += progression;
    }
    }

    Second script

    #pragma strict

    public var caseProgressor:CaseScript;

    function Start () {

    }

    function Update () {

    }

    function OnMouseDown () {
    caseProgressor.progress(5);
    }
     
  2. Avalion

    Avalion

    Joined:
    May 2, 2012
    Posts:
    34
    Hello.

    You are not using GUI elements here if I understand...

    I think the best way to do a ProgressBar is a simple GUI elements. I'm not familiar with Unity 4.6 GUI but I often use the GUI class and functions in my MonoBehaviours to display my 2D UI.

    To achieve a progressbar, this is really simple with this class ! Here is a sample of how to draw a ProgressBar in GUI (C#) :
    Code (CSharp):
    1.     private static GUIStyle ProgressBarBackground = new GUIStyle();
    2.     private static GUIStyle ProgressBarContent = new GUIStyle();
    3.     private bool ProgressBarStylesInit = false;
    4.  
    5.     public static void InitStyles() {
    6.         // Put here your styles values;
    7.         ProgressBarStylesInit = true;
    8.     }
    9.  
    10.     public static void ProgressBar(float _width, float _height, int _value, int _max, params GUILayoutOption[] _options) {
    11.         if (!ProgressBarStylesInit)
    12.             InitStyles();
    13.  
    14.         _value = Mathf.Clamp(_value, 0, _max);
    15.         GUILayout.BeginHorizontal(_options);
    16.         GUILayout.Label("", ProgressBarBackground, GUILayout.Width(_width));
    17.         GUILayout.Space(-_width - ProgressBarBackground.margin.left);
    18.         GUILayout.Label("", ProgressBarContent, GUILayout.Width(Mathf.CeilToInt(_width * _value / (float)_max)));
    19.         GUILayout.Space(_width - Mathf.CeilToInt(_width * _value / (float)_max));
    20.         GUILayout.EndHorizontal();
    21.     }
    When you know how to use the GUI class, this become simple to add a button when it's full, and do what you want.

    Hope this helps
     
    dandelionxxx likes this.