ProgressBar.cs 875 B

123456789101112131415161718192021222324252627282930
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class ProgressBar : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. public Image progressBarBody;
  10. public Image progressBarBackground;
  11. public int setValue=0;
  12. private float step;
  13. private float height;
  14. void Start()
  15. {
  16. step = progressBarBody.GetComponent<RectTransform>().rect.width / 100;
  17. height = progressBarBody.GetComponent<RectTransform>().rect.height;
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. float width = step * setValue;
  23. progressBarBody.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
  24. progressBarBody.GetComponent<RectTransform>().anchoredPosition = new Vector2(width/2,0);
  25. }
  26. }