FrameInterval.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. namespace UniRx.Operators
  3. {
  4. internal class FrameIntervalObservable<T> : OperatorObservableBase<UniRx.FrameInterval<T>>
  5. {
  6. readonly IObservable<T> source;
  7. public FrameIntervalObservable(IObservable<T> source)
  8. : base(source.IsRequiredSubscribeOnCurrentThread())
  9. {
  10. this.source = source;
  11. }
  12. protected override IDisposable SubscribeCore(IObserver<UniRx.FrameInterval<T>> observer, IDisposable cancel)
  13. {
  14. return source.Subscribe(new FrameInterval(observer, cancel));
  15. }
  16. class FrameInterval : OperatorObserverBase<T, UniRx.FrameInterval<T>>
  17. {
  18. int lastFrame;
  19. public FrameInterval(IObserver<UniRx.FrameInterval<T>> observer, IDisposable cancel)
  20. : base(observer, cancel)
  21. {
  22. this.lastFrame = UnityEngine.Time.frameCount;
  23. }
  24. public override void OnNext(T value)
  25. {
  26. var now = UnityEngine.Time.frameCount;
  27. var span = now - lastFrame;
  28. lastFrame = now;
  29. base.observer.OnNext(new UniRx.FrameInterval<T>(value, span));
  30. }
  31. public override void OnError(Exception error)
  32. {
  33. try { observer.OnError(error); }
  34. finally { Dispose(); }
  35. }
  36. public override void OnCompleted()
  37. {
  38. try { observer.OnCompleted(); }
  39. finally { Dispose(); }
  40. }
  41. }
  42. }
  43. }