Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using UnityEngine.UI;
- public class CoroutineTest : MonoBehaviour
- {
- public Image BarImage;
- private Coroutine _fillAmountCoroutine = null;
- [SerializeField] private float _decreaseStep = 0.2f;
- [SerializeField] private float _defaultEndValue = 1f;
- private float _endValue = 1f;
- private void Start()
- {
- ResetEndValue();
- }
- public void ResetEndValue()
- {
- _endValue = _defaultEndValue;
- }
- private void Update()
- {
- if (Mouse.current.leftButton.wasPressedThisFrame)
- {
- if (_fillAmountCoroutine != null)
- StopCoroutine(_fillAmountCoroutine);
- IEnumerator doFillAmount = DoFillAmount(BarImage, 0f, _endValue, 1f);
- _fillAmountCoroutine = StartCoroutine(doFillAmount);
- _endValue -= _decreaseStep;
- if (_endValue <= 0)
- ResetEndValue();
- }
- }
- private IEnumerator DoFillAmount(Image image, float start, float end, float duration)
- {
- float elapsed = 0.0f;
- while (elapsed < duration)
- {
- image.fillAmount = Mathf.Lerp(start, end, elapsed / duration);
- elapsed += Time.deltaTime;
- yield return null;
- }
- image.fillAmount = end;
- }
- }
Add Comment
Please, Sign In to add comment