Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- public class CounterScript : MonoBehaviour
- {
- [SerializeField] private Text counterText;
- private float counter = 0f;
- private bool isCounting = false;
- private Coroutine countCoroutine;
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- isCounting = !isCounting;
- if (isCounting)
- {
- if (countCoroutine != null)
- StopCoroutine(countCoroutine);
- countCoroutine = StartCoroutine(CountCoroutine());
- }
- else
- {
- if (countCoroutine != null)
- {
- StopCoroutine(countCoroutine);
- }
- }
- }
- }
- IEnumerator CountCoroutine()
- {
- while (isCounting)
- {
- counter++;
- counterText.text = counter.ToString();
- yield return new WaitForSeconds(0.5f);
- }
- }
- }
Advertisement
Comments
-
- Реализуйте счетчик, который каждые 0.5 сек. увеличивается на единицу.
- При нажатии на кнопку мыши счетчик начинает увеличиваться, при повторном нажатии останавливается.
- Значения счетчика не обнуляются, каждый раз продолжается со значения, на котором остановился.
- Для отображения значений счетчика используйте Debug.Log или можете использовать элемент UI, а именно Text.
- Для решения задачи используйте корутину.
Add Comment
Please, Sign In to add comment
Advertisement