Skip to content

Instantly share code, notes, and snippets.

@QRemark
Created September 1, 2024 14:47
Show Gist options
  • Select an option

  • Save QRemark/38a5f0d52902cb01d49f873ced3f24b4 to your computer and use it in GitHub Desktop.

Select an option

Save QRemark/38a5f0d52902cb01d49f873ced3f24b4 to your computer and use it in GitHub Desktop.
Счетчик
using System;
using System.Collections;
using UnityEngine;
public class Score : MonoBehaviour
{
private bool _isRunning = true;
private int _score = 1;
private int _leftMouseButton = 0;
private Coroutine _countdownCoroutine;
public event Action<int> OnScoreChanged;
private void Start()
{
_countdownCoroutine = StartCoroutine(Countdown(0.5f));
}
private void Update()
{
if (Input.GetMouseButtonDown(_leftMouseButton))
{
if (_isRunning)
{
_isRunning = false;
if (_countdownCoroutine != null)
{
StopCoroutine(_countdownCoroutine);
_countdownCoroutine = null;
}
}
else
{
_isRunning = true;
_countdownCoroutine = StartCoroutine(Countdown(0.5f));
}
}
}
private IEnumerator Countdown(float delay)
{
var wait = new WaitForSecondsRealtime(delay);
while (true)
{
OnScoreChanged?.Invoke(_score++);
yield return wait;
}
}
}
using TMPro;
using UnityEngine;
public class ScoreView : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _text;
[SerializeField] private Score _score;
private void OnEnable()
{
_score.OnScoreChanged += UpdateScoreText;
}
private void OnDisable()
{
_score.OnScoreChanged -= UpdateScoreText;
}
private void UpdateScoreText(int score)
{
_text.text = "Счетчик ДЗ: " + score.ToString("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment