UniRx - Introduction
Updated at 2017-07-27 19:52
UniRx (Reactive Extensions for Unity) is a reimplementation of the .NET Reactive Extensions. It vastly simplifies asynchronous operations and provides an awesome collection of helpers such as pooling and tuples.
Reactive programming is worth learning. Reactive programming means dealing with asynchronous data streams. The concept is easy to understand but using reactive programming is harder, it's whole new paradigm after all.
- Reactive code is more concise than normal event handling.
- Makes it easy to turn events to other events (stream => stream).
- Turns complex rules like "click three times in 2 seconds" one-liners, thus making it excellent workhorse for game code.
Reactive programming is immutable. All functions never modify the original stream but return a new one.
Terminology:
- Observables or streams are collections of sequential events.
- Metastreams means a stream of streams.
- Subscribing is the action to start listening to a stream.
- Observer or subscriber defines how to act on the events stream produces.
Streams have three possible actions from observers point-of-view:
- Emit an item; invoking
OnNext
on all subscribers. - Error notify; invoking
OnError
on all subscribers. - Complete notify; invoking
OnCompleted
on all subscribers.