Skip to content

Instantly share code, notes, and snippets.

@shohan4556
Created August 4, 2024 07:58
Show Gist options
  • Save shohan4556/3f15e9ef5734adc0015bf706b47d163c to your computer and use it in GitHub Desktop.
Save shohan4556/3f15e9ef5734adc0015bf706b47d163c to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
public class ProgressBar : MonoBehaviour
{
public Slider progressBar; // The UI slider component
public int currentLevel = 1;
public int maxLevel = 100;
private int progressInCurrentLevel = 0; // Progress within the current level
private int maxProgressInLevel = 10; // Maximum progress within a level before it resets
void Start()
{
// Initialize the progress bar
UpdateProgressBar();
}
public void IncreaseProgress(int amount)
{
// Increase the progress within the current level
progressInCurrentLevel += amount;
// Check if progress exceeds the current level's max progress
if (progressInCurrentLevel >= maxProgressInLevel)
{
// Move to the next level
currentLevel++;
// If we exceed the maximum level, clamp it to max level
if (currentLevel > maxLevel)
{
currentLevel = maxLevel;
}
// Reset the progress in the current level
progressInCurrentLevel = 0;
}
// Update the progress bar
UpdateProgressBar();
}
private void UpdateProgressBar()
{
// Calculate the progress percentage within the current level
float progressPercentage = (float)progressInCurrentLevel / maxProgressInLevel;
progressBar.value = progressPercentage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment