Advertisement
Pro_Unit

FillAmoutImage

Jan 3rd, 2022
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6. using UnityEngine.UI;
  7.  
  8. public class CoroutineTest : MonoBehaviour
  9. {
  10.     public Image BarImage;
  11.     private Coroutine _fillAmountCoroutine = null;
  12.  
  13.     [SerializeField] private float _decreaseStep = 0.2f;
  14.     [SerializeField] private float _defaultEndValue = 1f;
  15.     private float _endValue = 1f;
  16.  
  17.     private void Start()
  18.     {
  19.         ResetEndValue();
  20.     }
  21.  
  22.     public void ResetEndValue()
  23.     {
  24.         _endValue = _defaultEndValue;
  25.     }
  26.  
  27.     private void Update()
  28.     {
  29.         if (Mouse.current.leftButton.wasPressedThisFrame)
  30.         {
  31.             if (_fillAmountCoroutine != null)
  32.                 return;
  33.  
  34.             IEnumerator doFillAmount = DoFillAmount(BarImage, 0f, 1, 1f);
  35.  
  36.             _fillAmountCoroutine = StartCoroutine(doFillAmount);
  37.         }
  38.     }
  39.  
  40.     private IEnumerator DoFillAmount(Image image, float start, float end, float duration)
  41.     {
  42.         float elapsed = 0.0f;
  43.         while (elapsed < duration)
  44.         {
  45.             image.fillAmount = Mathf.Lerp(start, end, elapsed / duration);
  46.             elapsed += Time.deltaTime;
  47.             yield return null;
  48.         }
  49.  
  50.         image.fillAmount = end;
  51.         _fillAmountCoroutine = null;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement