In part 1 of Learning UniRx we learned that reactive programming is
programming with data streams. In UniRx data streams are known as Observables.
In this video we’ll be taking a closer look at what Observables are, how they’re
implemented, and some of the Unity specific Observables that are provided
by UniRx. An Observable represents a sequence of events. Each event results in
the Observable emitting a value. Observers subscribe to Observables, and
react to the values that they emit. Every flavor of Reactive Extensions must
follow a contract that defines how Observables should be implemented. The
contract is as follows. An Observable should communicate with its observers
via the following notifications. OnNext tells the observer that an item has been
emitted. OnCompleted tells the observer that the Observable has completed
successfully, and will no longer be emitting values. And OnError tells the
observer that the Observable has been terminated do to a specified error, and
will no longer be emitting values. Furthermore, the contract states that an
observer will communicate with an Observable via the following
notifications. Subscribe tells the Observable that the
observer is ready to receive notifications. And Unsubscribe tells
the Observable that the observer no longer wants to receive notifications. This
custom Observable, created using the static Create method, gives us a better
idea of how these notifications are called under the hood. Create takes a
delegate method that provides access to the observer. This is where you can call
OnNext, OnCompleted, and OnError. In the case of our example, the delegate loops
through an array of numbers, emitting each value and then waiting for one
second. Then it calls OnCompleted to let the observer know that the Observable has
completed successfully, and will no longer be emitting values. The observer
that’s subscribed to this custom Observable simply writes the emitted
values to the console. As you learn more about Observables and their operators,
you’ll want to become more familiar with marble diagrams. Marble diagrams help you
isualize what Observables are actually emitting. This is a marble diagram of the
custom Observable from the previous example. The circles, or marbles, represent
the values that his Observable emits. The line that they sit on represents the
flow of time. Remember, these values are emitted one second apart from each other.
The line at the end of the marble diagram represents the completion of the
Observable. Now, the example didn’t include an error, but if it did,
it would be represented by an X, like so. Soon I’ll cover operators, and how they filter,
transform, and combine Observables. These can all be visualized by marble diagrams,
as well. Take the Select operator The emitted values flow into this box, that
represents the transformation. Then they flow down into a new Observable that
is visualized by it’s own marble diagram. Marble diagrams are powerful tools that
can help you to fully grasp and understand how the more complicated operators are
affecting your Observables. So far we’ve talked about what Observables are and
how we can create our own. But for the most part, you’ll generally want to rely
on the Observables that are provided by UniRx. These built in Observables, which
are known as Triggers, are specific to Unity, turning MonoBehaviour events
such as Update and OnCollisionEnter into streams ff data that you can
subscribe to. This simple example shows a MonoBehaviour that logs the name of
each gameObject that it collides with. First, it needs to import UniRx.Triggers
Then it subscribes to OnCollisionEnterAsObservable in it’s Start method,
where it calls Debug.Log, passing in the name of the collision’s gameObject.
This is the naming convention that is used for all MonoBehaviour events, with
the name of the event followed by “AsObservable”. From Updates to Collisions to
Mouse events, UniRx comes packaged with support for all of the most commonly
used MonoBehaviour events. There are also a couple of other Unity specific gems, such as
Reactive Properties, that I’ll cover in another video. In the next video, we'll be
talking about the different types of operators that are available in UniRx.
Operators allow you to filter, transform, and combine your Observables, giving you
more fine-grained control over how you compose your asynchronous code. If you
enjoyed this video, please leave a like and a comment letting me know what you thought.
And for more Unity tutorials just like this one please subscribe
with notifications on. And if you'd like to support the creation of content like
this please consider becoming a patron on the Infallible Code Patreon page.