Let the magic happen when you are not watching

Reactive Programming in Unity Engine with UniRx

Looking back at my life, I must say that I’ve never skipped an opportunity to cut corners. It might sound weird but at no time I considered that as a bad trait. Having in mind Bill Gates words “I choose a lazy person to do a hard job because a lazy person will find an easy way to do it”, some may even say that this is a pretty cool superpower ✨ Due to this fact, Reactive Programming and I became quite close friends. Suddenly I started being more effective without any additional effort. Grab a cup of coffee and see for yourself why!

What kind of sorcery is that?

Reactive Programming is definitely a trending topic among coders. It’s hardly surprising that more and more people are getting familiar with it and apply it in their work environment.

UniRx is a free library, available in Asset Store, that helps you implement reactive architecture in Unity Engine and totally changes feelings for asynchronous operations and event-based actions in games or apps (ekhm… for the better, obviously! 😎).

The whole concept is based on Observer Design Pattern but the actual implementation is far more advanced. Hey, don’t panic there, buddy! Advanced does not mean harder to understand! For you, it means fewer things to care about and a lot of easy-to-use tools that will do all the dirty work instead of you.

Furthermore, reactive programming goes well with design patterns like Model-View-ViewModel Design Pattern, which also allows you to write tests easier than ever before! Communication process relies on subscriptions and is carried out using 3 types of objects:

Subjects : Subject is a source of values that objects outside the class need.

: Subject is a source of values that objects outside the class need. Observables : Observable serves as a data stream that passes values from Subject to objects already subscribed to this specific stream. It can be upgraded with Operators that can manipulate data before pass or change condition on which data is passed.

: Observable serves as a data stream that passes values from Subject to objects already subscribed to this specific stream. It can be upgraded with Operators that can manipulate data before pass or change condition on which data is passed. Observers: Observer works as an entry point for other classes that can force Subject to emit specific value.

Tools that come in handy.

Observables are crucial communication elements in reactive programming but their true power lies in synergy with Operators that can significantly change data flow or even mutate data emitted by other Observables into other types after certain conditions are met! The most common Operators are:

Where : Allows you to filter data with a specific condition.

: Allows you to filter data with a specific condition. Select : Allows you to expose and emit a specific part of the data.

: Allows you to expose and emit a specific part of the data. Skip : Skips a given amount of data emissions.

: Skips a given amount of data emissions. WhenAll : Is called only when all given asynchronous operations are finished.

: Is called only when all given asynchronous operations are finished. TakeUntil: Emits data until specific stream starts data emission.

Emits data until specific stream starts data emission. Delay : Delays data emission by given time.

: Delays data emission by given time. Throttle: Emits data when a particular time span has passed.

And many many more! Basically, every time you want to do something, there is probably an operator that already does it! The longer you explore, the more powerful tools you find! 💪

Get ready for action!

To prove how easy-to-use UniRx is, I’ll show you a quick example. In 5 simple steps, we are going to create a simple counter that calls method every time count changes and eventually we will filter data to make calls only with the even count.

Create Subject that emits count.

2. Create Observable so we can access count stream.

3. Subscribe Observable to receive value every time count changes.

4. Emit count to trigger subscribers.

5. Add Operator Where to make calls only when the count is even.

And voila! Communication established! It was pretty simple, wasn’t it? Are you not convinced yet? Look through other implementations that can be found in “Sticker Man” Customization Popup project on our GitHub!

Are you hungry for more?

To start getting familiar with Reactive Programming I would recommend ReactiveX website to understand the whole theory behind it and RxMarbles to explore how different operators may affect data flow and cover all the dirty work for you. Don’t hesitate to discover technology that can benefit every professional coder! 😏

Don’t forget to tap and hold 👏 and to follow DaftMobile Blog (just press “Follow” button below)! You can also find us on Facebook and Twitter.