Creating a Level Bar Progress in Unity using C#

Creating a Level Bar Progress in Unity using C#

  1. Create a new Unity project and add a UI Image component to the scene. This image will serve as the background for the level bar.

  2. Create a new C# script and name it "LevelBar." Attach the script to the UI Image game object.

  3. In the LevelBar script, create a public float variable named "currentProgress" and set it to 0. This variable will be used to store the current progress of the level bar.

  4. Create a new UI Slider component and set its "Fill Area" to the UI Image game object. This will make the slider fill the image, creating the appearance of a level bar.

  5. In the LevelBar script, create a public function named "UpdateProgress" that takes in a float value as a parameter. This function will be used to update the current progress of the level bar.

  6. Inside the UpdateProgress function, set the "currentProgress" variable to the value passed in as a parameter. Then, set the "value" property of the UI Slider component to the current progress.

  7. In a separate script or in the Update function of the LevelBar script, call the UpdateProgress function and pass in a new value to update the level bar's progress.

  8. Test the level bar by running the Unity project and observing the changes in the level bar's progress as the UpdateProgress function is called with different values.

A basic  C# code to use:



using UnityEngine;
using UnityEngine.UI;

public class LevelBar : MonoBehaviour
{
    public float currentProgress = 0;
    public Slider progressBar;

    public void UpdateProgress(float progress)
    {
        currentProgress = progress;
        progressBar.value = currentProgress;
    }
}




In this code, the LevelBar script is attached to the UI Image game object and the progressBar variable is linked to the UI Slider component. The UpdateProgress function updates the currentProgress variable and the value of the progressBar Slider component.

An advanced version of the code that includes additional functionality:

using UnityEngine;
using UnityEngine.UI;

public class LevelBar : MonoBehaviour
{
    public float maxProgress = 100;
    public float currentProgress;
    public Slider progressBar;
    public Image progressBarFill;
    public Color lowProgressColor = Color.red;
    public Color fullProgressColor = Color.green;
    public GameObject levelCompleteEffect;
    public AudioClip levelUpSound;
    public AudioClip levelCompleteSound;
    public float levelUpSoundVolume = 0.5f;
    public float levelCompleteSoundVolume = 1f;

    private AudioSource audioSource;

    void Start()
    {
        currentProgress = 0;
        progressBar.maxValue = maxProgress;
        progressBar.value = currentProgress;
        progressBarFill.color = lowProgressColor;
        audioSource = GetComponent<AudioSource>();
        if(audioSource == null)
        {
            audioSource = gameObject.AddComponent<AudioSource>();
        }
    }

    public void AddProgress(float progress)
    {
        currentProgress += progress;
        progressBar.value = currentProgress;
        UpdateProgressBarColor();
        if(currentProgress >= maxProgress)
        {
            CompleteLevel();
        }
        else
        {
            if(levelUpSound != null)
            {
                audioSource.PlayOneShot(levelUpSound, levelUpSoundVolume);
            }
        }
    }

    public void UpdateProgress(float progress)
    {
        currentProgress = progress;
        progressBar.value = currentProgress;
        UpdateProgressBarColor();
        if(currentProgress >= maxProgress)
        {
            CompleteLevel();
        }
    }

    void UpdateProgressBarColor()
    {
        float progressPercent = currentProgress / maxProgress;
        if(progressPercent >= 0.7f)
        {
            progressBarFill.color = fullProgressColor;
        }
        else
        {
            progressBarFill.color = lowProgressColor;
        }
    }

    void CompleteLevel()
    {
        if(levelCompleteEffect != null)
        {
            Instantiate(levelCompleteEffect, transform.position, transform.rotation);
        }
        if(levelCompleteSound != null)
        {
            audioSource.PlayOneShot(levelCompleteSound, levelCompleteSoundVolume);
        }
        // You can add here additional functionality like loading a new scene or disabling some gameObjects
    }
}


The new features are:
  • A maxProgress variable to store the maximum progress of the level bar
  • A progressBarFill variable to store the Image component that fills the Slider to change its color
  • A lowProgressColor variable to store a color to change the progress bar fill color when the progress is low
  • A levelCompleteEffect variable to store a GameObject that will be instantiated when the level is completed
  • A levelUpSound variable to store an AudioClip that will be played when the progress is increased
  • A levelCompleteSound variable to store an AudioClip that will be played when the level is completed
  • A levelUpSoundVolume and levelCompleteSoundVolume variable to adjust the volume of the sound effects
  • An AudioSource component that will play the