Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class ModuloBar : MonoBehaviour
- {
- [SerializeField] Slider progressBar;
- [SerializeField] Text levelTier;
- [SerializeField] int maxTier = 6; //max amount per tier, always set this value to the visual bar + 1
- [SerializeField] int statValue = 0; //your real progress value
- [SerializeField] Button addBtn, subBtn;
- // Start is called before the first frame update
- void Start()
- {
- UpdateProgressBar();
- addBtn.onClick.AddListener(IncreaseProgress);
- subBtn.onClick.AddListener(DecreaseProgress);
- }
- //Method for updating the progress bar based on the real progress value (statValue)
- private void UpdateProgressBar()
- {
- progressBar.value = statValue % maxTier; //Increase the bar value using modulo, so it resets when reach the max tier amount
- levelTier.text = Mathf.Floor(statValue / ((float)maxTier)).ToString(); //Use floor operation to define our current tier
- }
- //Method for increasing progress
- void IncreaseProgress()
- {
- statValue++;
- UpdateProgressBar();
- }
- //Method for decreasing progress, and clamp the value, so it doesn't go below the minimum value (in this case 0)
- void DecreaseProgress()
- {
- statValue--;
- statValue = Mathf.Clamp(statValue, 0, int.MaxValue);
- UpdateProgressBar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement