Advertisement
_EagleOwle_

Timer

Jun 24th, 2021
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6.  
  7. public class Timer : MonoBehaviour
  8. {
  9.     #region SINGLETON
  10.     private static Timer _singleton;
  11.     public static Timer Singleton
  12.     {
  13.         get
  14.         {
  15.             if (_singleton == null)
  16.             {
  17.                 _singleton = GameObject.FindObjectOfType<Timer>();
  18.             }
  19.             return _singleton;
  20.         }
  21.     }
  22.     #endregion
  23.  
  24.     public UnityEvent timeEvent;
  25.  
  26.     public float Seconds;
  27.     public int Hours;
  28.     public int Minutes;
  29.  
  30.     [SerializeField] private Text m_text;
  31.  
  32.     private bool eventIsOn = true;
  33.  
  34.     private void Start()
  35.     {
  36.         if (timeEvent == null)
  37.             timeEvent = new UnityEvent();
  38.  
  39.         StartTimer();
  40.     }
  41.  
  42.     public void StartTimer()
  43.     {
  44.         StopAllCoroutines();
  45.         StartCoroutine(TimerRoutine());
  46.     }
  47.  
  48.     public void StopTimer()
  49.     {
  50.         StopAllCoroutines();
  51.         GetCurrentTime();
  52.     }
  53.  
  54.     private IEnumerator TimerRoutine()
  55.     {
  56.         Seconds = 0;
  57.         Hours = 0;
  58.         Minutes = 0;
  59.         while (true)
  60.         {
  61.             Seconds += Time.deltaTime;
  62.  
  63.             if(IsDivisble((int)Seconds, 10))
  64.             {
  65.                 if (eventIsOn == false)
  66.                 {
  67.                     timeEvent.Invoke();
  68.                     eventIsOn = true;
  69.                 }
  70.             }
  71.             else
  72.             {
  73.                 eventIsOn = false;
  74.             }
  75.  
  76.             if (Seconds >= 60)
  77.             {
  78.                 Seconds -= 60.0f;
  79.                 Minutes += 1;
  80.                 if (Minutes >= 60)
  81.                 {
  82.                     Minutes -= 60;
  83.                     Hours += 1;
  84.                 }
  85.             }
  86.  
  87.             GetCurrentTime();
  88.             yield return null;
  89.         }
  90.     }
  91.  
  92.     public void GetCurrentTime()
  93.     {
  94.         //m_text.text = $"{Hours:00}:{Minutes:00}:{Seconds:00}";
  95.         m_text.text = $"Time: {Minutes:00}:{Seconds:00}";
  96.     }
  97.  
  98.     /// <summary>
  99.     /// Проверка на кратность одного числа на другое
  100.     /// </summary>
  101.     /// <param name="x">Исходное число</param>
  102.     /// <param name="n"></param>
  103.     /// <returns></returns>
  104.     public bool IsDivisble(int x, int n)
  105.     {
  106.         return (x % n) == 0;
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement