neuecc / UniRx

Reactive Extensions for Unity
MIT License
7.01k stars 895 forks source link

Observable.EveryUpdate() keeps being called even after exiting play mode #472

Open darthdeus opened 3 years ago

darthdeus commented 3 years ago

Running the following causes the observable to keep running even after exiting play mode. If I run the game repeatedly it continues to spawn more observables and they all keep logging.

Observable.EveryUpdate().Subscribe(x => Debug.Log(x));

The README says

Other static generator methods (Observable.Timer, Observable.EveryUpdate, etc...) do not stop automatically, and their subscriptions should be managed manually.

which makes sense, but at least personally I find it surprising that the observable keeps running even after play mode is exited. Is this actually intentional? If it is, it might be worth adding a note to the README about this behavior. (If I knew how exactly it behaves I'd gladly make a PR, but I'm not really sure what is supposed to happen).

benjaminSponagel commented 3 years ago

Hello, although this issue has been opened some time ago and I'm not going to address the README part, I'd like to mention, that you can Dispose() observables in MonoBehaviour.OnDestroy() to stop this unwanted behavior.

scho commented 3 years ago

Using UniRx.DisposableExtensions.AddTo helps in this case.