Reactive Programming,
Unity 3D and you
Quick tutorial for using UniRx
by Sam Megidov
What's UniRx?
- It's a re-implementation of NET Reactive Extensions (ReactiveX) for Unity3D by Yoshifumi Kawai.
ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.
So what’s the use of it?
- It makes your life easier !
- Makes all properties and actions plug-and-play.
- Clean and more manageable code.
Let's see some examples
Update score text every time score changes
var score = new ReactiveProperty<Int>(0)
score.SubscribeToText(scoreText)
Now every time score changes,
we update text in the scoreText GameObject.
No need to put that code into an update loop !
How about a highscore?
- That's it !
- It compares every value and gets the highest number.
score
.Scan(int.MinValue, Mathf.Max)
.SubscribeToText (highscoreText)
How about something more complicated?
var scoreDelayed = score.Throttle(TimeSpan.FromMilliseconds(500))
.ToReactiveProperty()
Accumulates amount of added score with a window of 500ms.
scoreDelayed.SubscribeToText(scoreText)
score.Select(x => x - scoreDelayed.Value)
.SubscribeToText(scoreDeltaText)
Advantages ?
- Ultra fast performance !
- Make sequences, delay them, buffer them, and more !
- Works on all platforms, full source included.
- Constantly updated, 2 years of updates and counting.
Limitations ?
- Still lacks some operators, but most useful ones are already there.
- Not enough documentation for game development.
- Takes a bit of time to get used to it.
Thank you
Sam Megidov
https://be.linkedin.com/in/sammeg
Reactive Programming, Unity 3D and you
By Sam Megidov
Reactive Programming, Unity 3D and you
Quick tutorial for using UniRx in Game Development
- 21,444