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. Dismiss Notice

Fill Progress Bar Over Time

Discussion in 'Scripting' started by mholmes, Mar 2, 2020.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Anyone have an example of how to fill a progress bar over time? Building a crafting system and I want to wait until 1.5 seconds has elapsed and fill a progress bar.
     
  2. Tobs-

    Tobs-

    Joined:
    Feb 12, 2016
    Posts:
    17
    You could use a Coroutine: something like that

    Code (CSharp):
    1. IEnumerator(ProgressbarRef, duration)
    2. {
    3.    float time = 0.0f;
    4.    while(time < duration)
    5.    {
    6.       ProgressbarRef.progress = time / Mathf.Max(duration, SmallEpsilon);
    7.       time += Time.deltaTime;
    8.       yield return new WaitEndOfFrame();
    9.    }
    10. }
    Now whenever you need to start a loading process on a progressbar object you call: StartCoroutine(progressbar, 1.5f)

    I did not test this, but it might give you a starting point.

    Cheers,
    Tobi
     
    stroibot likes this.
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    SmallEpsilon? this the amount you increase the progress bar value by?
     
  4. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    It's a small value to handle the case where duration is 0. If duration was ever 0, then you'd get a DivideByZero exception.
     
  5. Rocky_Unity

    Rocky_Unity

    Joined:
    Oct 13, 2017
    Posts:
    96
    This is awesome, thanks for the question @mholmes and the answer @Tobs and the clarification @kru
    What a fantastic community :)
     
  6. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Here is what I created based on help:

    Code (CSharp):
    1. IEnumerator Craft_Item(int duration, int TotalAmount)
    2.     {
    3.         //Base Crafting System
    4.         Core_Functions core = new Core_Functions();
    5.         //Base Crafting Item Return
    6.         Base_Crafting_Item_Return base_return = new Base_Crafting_Item_Return();      
    7.  
    8.         //For Each Item To Craft
    9.         for (int i = 0; i < TotalAmount; i++)
    10.         {          
    11.             bool hasingrediants = _Crafting_Functions.Ingrediant_Check();
    12.  
    13.             //Ingrediants Check
    14.             if (hasingrediants)
    15.             {
    16.                 //Display Progress In Text
    17.                 Crafting_Data._CurrentAmount = i;
    18.                 _CraftAmount.text = (Crafting_Data._CurrentAmount + 1).ToString() + "/" + Crafting_Data._TotalAmount.ToString();
    19.                 base_return = core.Craft_Item(Crafting_Data._itemname, Crafting_Data._Player_Skill_Level);
    20.  
    21.                 //Crafted Item Successfully
    22.                 if (base_return.Success)
    23.                 {
    24.                     FindObjectOfType<AudioManager>().Play("Craft_Rune_Success");
    25.                 }
    26.                 //Failed To Craft Item
    27.                 else
    28.                 {
    29.                     FindObjectOfType<AudioManager>().Play("Craft_Rune_Failed");
    30.                 }
    31.  
    32.                 //Progress Bar Display
    33.                 while (_Crafting_Progress < duration)
    34.                 {
    35.                     _pbCrafting.value = _Crafting_Progress;
    36.                     _Crafting_Progress += 0.1f;
    37.                     yield return new WaitForSecondsRealtime(0.1f);
    38.                 }
    39.  
    40.                 //Consume Ingrediants
    41.                 Consume_Ingrediants();
    42.                 //Craft Item
    43.                 if (base_return.Success) { _Crafting_Functions.Update_Craft_Inventory(Crafting_Data._itemname, base_return.Bonus); }
    44.                 //Reset Progress
    45.                 _Crafting_Progress = 0.00f;
    46.             }
    47.             else
    48.             {
    49.                 _CraftAmount.text = "Missing Required Ingredients";
    50.                 FindObjectOfType<AudioManager>().Play("Crafting_Error");
    51.                 _pbCrafting.value = 0;
    52.                 _Crafting_Progress = 0;
    53.                 _pbCrafting.gameObject.SetActive(false);
    54.                 yield return new WaitForSecondsRealtime(3.5f);
    55.                 break;
    56.             }
    57.         }
    58.  
    59.         //Reset Display
    60.         Crafting_Data._IsCrafting = false;
    61.         _pbCrafting.value = 0;
    62.         _Crafting_Progress = 0;
    63.         _CraftAmount.text = "Amount";
    64.         _pbCrafting.gameObject.SetActive(false);
    65.         _btnCraftItem.enabled = true;
    66.         _CraftAmount.readOnly = false;
    67.     }
    This is a nearly complete crafting system.