Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- namespace GameCore
- {
- public class OnTicksTimer : MonoBehaviour
- {
- [SerializeField] UnityEvent OnStartTimer;
- [SerializeField] UnityEventInt OnTickTimer;
- [SerializeField] UnityEvent OnEndTimer;
- [SerializeField] int Seconds;
- public void StartTimer()
- {
- StartTimer(0);
- }
- public void StartTimer (int seconds = 0)
- {
- if (seconds != 0) Seconds = seconds;
- OnStartTimer.Invoke ();
- StartCoroutine (TickTimer ());
- }
- private IEnumerator TickTimer ()
- {
- if (CheckComplete ()) yield break;
- Seconds = Seconds.Abs (); // Seconds = Mathf.Abs(Seconds); // Is to need positive
- WaitForSeconds wfs = new WaitForSeconds (1f);
- while (Seconds > 0)
- {
- yield return wfs;
- Seconds--;
- OnTickTimer.Invoke (Seconds);
- if (CheckComplete ()) yield break;
- }
- }
- private bool CheckComplete ()
- {
- bool isZero = Seconds == 0;
- if (isZero) OnEndTimer.Invoke ();
- return isZero;
- }
- }
- [Serializable] public class UnityEventInt : UnityEvent<int> { }
- }
Add Comment
Please, Sign In to add comment