Created
September 1, 2024 09:32
-
-
Save QRemark/c02908d732e3a2758163677ea3da53dd to your computer and use it in GitHub Desktop.
Счетчик
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using UnityEngine; | |
| public class Score : MonoBehaviour | |
| { | |
| public event Action<int> OnScoreChanged; | |
| private bool _isRunning = true; | |
| private int _score = 1; | |
| private int _leftMouseButton = 0; | |
| private Coroutine _countdownCoroutine; | |
| 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); | |
| for (int i = _score; i > 0; i++) | |
| { | |
| _score++; | |
| OnScoreChanged?.Invoke(_score); | |
| yield return wait; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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