Pro_Unit

ObervableTimer

May 10th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using GameCore;
  2. using GameCore.CrossPlatfomADMediator;
  3.  
  4. using System;
  5.  
  6. using UniRx;
  7.  
  8. using Unity.Linq;
  9.  
  10. using UnityEngine;
  11. using Text = TMPro.TextMeshProUGUI;
  12. namespace QuestEngine
  13. {
  14.     public class RewardedAdsTimer : MonoBehaviour
  15.     {
  16.         [SerializeField] private Text _text;
  17.         [SerializeField] private ADListener _ads;
  18.         [SerializeField] private IntVariable _secondsFromWait;
  19.  
  20.         private TimeSpan _second = TimeSpan.FromSeconds(1);
  21.         private TimeSpan _waitTime;
  22.         private void Awake()
  23.         {
  24.  
  25.             _waitTime = TimeSpan.FromSeconds(_secondsFromWait);
  26.  
  27.             Observable
  28.                 .Timer(_waitTime, _second)
  29.                 .Subscribe(new ObervableTimer(DisableText, UpdateText, error => DisableText()));
  30.  
  31.         }
  32.  
  33.         private void DisableText()
  34.         {
  35.             ActiveChids(false);
  36.         }
  37.         private void UpdateText(TimeSpan timer)
  38.         {
  39.             _text.text = timer.ToString(@"mm\:ss");
  40.         }
  41.  
  42.         private void ActiveChids(bool active)
  43.         {
  44.             gameObject.Children().ForEach(go => go.SetActive(active));
  45.         }
  46.     }
  47.  
  48.     public class ObervableTimer : IObserver<long>
  49.     {
  50.  
  51.         private readonly Action _onCompleted;
  52.         private readonly Action<TimeSpan> _onUpdate;
  53.         private readonly Action<Exception> _onError;
  54.  
  55.         public ObervableTimer(Action onCompleted, Action<TimeSpan> onUpdate = null, Action<Exception> onError = null)
  56.         {
  57.             _onCompleted = onCompleted;
  58.             _onUpdate = onUpdate;
  59.             _onError = onError;
  60.         }
  61.  
  62.         public void OnCompleted()
  63.         {
  64.             _onCompleted?.Invoke();
  65.         }
  66.  
  67.         public void OnError(Exception error)
  68.         {
  69.             _onError?.Invoke(error);
  70.         }
  71.  
  72.         public void OnNext(long tiks)
  73.         {
  74.             _onUpdate?.Invoke(TimeSpan.FromTicks(tiks));
  75.         }
  76.  
  77.     }
  78.  
  79. }
Add Comment
Please, Sign In to add comment