Created
September 1, 2024 14:47
-
-
Save QRemark/38a5f0d52902cb01d49f873ced3f24b4 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 | |
| { | |
| 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; | |
| } | |
| } | |
| } |
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