Reactive Programming by UniRx for Asynchronous & Event Processing

42,657 views

Published on

Unity Asset LT
https://github.com/neuecc/UniRx/

Published in: Technology
3 Comments
47 Likes
Statistics
Notes
  • Getting Started with Processing: A Hands-On Introduction to Making Interactive Graphics --- http://amzn.to/1pC7lY0
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Learning Processing, Second Edition: A Beginner's Guide to Programming Images, Animation, and Interaction (The Morgan Kaufmann Series in Computer Graphics) --- http://amzn.to/1RsFJki
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Processing: A Programming Handbook for Visual Designers and Artists (MIT Press) --- http://amzn.to/1UdwVPX
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
42,657
On SlideShare
0
From Embeds
0
Number of Embeds
34,002
Actions
Shares
0
Downloads
39
Comments
3
Likes
47
Embeds 0
No embeds

No notes for slide

Reactive Programming by UniRx for Asynchronous & Event Processing

  1. 1. Reactive Programming by UniRx for Asynchronous & Event Processing 2014/07/30 Yoshifumi Kawai - @neuecc
  2. 2. Self Introduction @ CTO C# 5.0 + .NET Framework 4.5 + ASP.NET MVC 5 C# Web @ Microsoft MVP for Visual C# / C# Web http://neue.cc/ Twitter @neuecc
  3. 3. What’s Reactive Programming?
  4. 4. Functional Reactive Programming http://en.wikipedia.org/wiki/Functional_reactive_programming Excel …… Reactive Extensions FRP (Haskell ) 2009 .NET Reactive Extensions
  5. 5. Functional Reactive Programming http://en.wikipedia.org/wiki/Functional_reactive_programming Excel …… Reactive Extensions FRP (Haskell ) 2009 .NET Reactive Extensions UniRx ReactiveExtensions(Rx) Unity
  6. 6. Gartner’s Hype Cycle 2013 Application Architecture/Application Development On the Rise - Reactive Programming
  7. 7. Technology Radar Languages & Framework ThoughtWorks Technology Radar July 2014 Rx ADOPT OK TRIAL ASSESS HOLD
  8. 8. Across Languages RxJava Netflix for Java/Scala ReactiveCocoa GitHub 5073 for Objective-C RxJS/bacon.js JavaScript RxJS bacon.js
  9. 9. UniRx
  10. 10. UniRx 100 Asset Store Reactive Programming Reactive Extensions RxJava Wiki
  11. 11. UniRx - Reactive Extensions for Unity
  12. 12. Reactive Extensions(Rx) is LINQ C# LINQ LINQ(to Objects) C# Rx LINQ LINQ Reactive Programming Rx
  13. 13. UniRx is .NET(C#) Rx Unity https://github.com/neuecc/UniRx http://u3d.as/7tT (Asset Store, Price : FREE) Rx + Unity C# …… https://rx.codeplex.com/ Bart De Smet Microsoft Unity :)
  14. 14. Q C# LINQ Rx 5 …… 3 @IT http://www.atmarkit.co.jp/fdotnet/introrx/introrx_01/introrx_01_01.html AOT Safe LINQ iOS AOT Unity + iOS AOT http://neue.cc/2014/07/01_474.html
  15. 15. Reactive Extensions is LINQ to Events LINQ to Asynchronous
  16. 16. Event is Observable Sequence Rx IObservable<T> time OnTap 3 Tap 5 Tap 1 Tap
  17. 17. IEnumerable <-> IObservable IObservable<T> time IEnumerable<T> length IEnumerable<T> length .Where(x => x % 2 == 0) IObservable<T> time .Where(x => x % 2 == 0) Where LINQ
  18. 18. Push Event Stream Event Processing Interactive/Visualize
  19. 19. Internet of Things Push Kinect Oculus Twitter Streaming PubSub, WebSocket Logs are Stream(Fluentd, Amazon Kinesis, Azure Event Hubs) MonoBehaviour Update , OnMouseClick, etc...
  20. 20. LINQ
  21. 21. LINQ LINQ LINQ
  22. 22. Better EventHandling
  23. 23. Limitations of .NET Events // public event Action<int> OnHitDamage; // player.OnHitDamage += (damage) => { if (damage >= 1000) { // " " } }; player.OnHitDamage -= /* */
  24. 24. Observable Sequence to the Rescue IObservable<int> onHitDamage = player.OnHitDamage; var criticalHit = onHitDamage .Where(x =>x >= 1000); var subscription = criticalHit.Subscribe(damage => /* ... */); subscription.Dispose(); LINQ
  25. 25. Lifecycle Resource Management // Disposable CompositeDisposable subscriptions = new CompositeDisposable(); void Awake() { var player = new Player(); var enemy1 = new Player(); var enemy2 = new Player(); // player.OnHitDamage.Subscribe().AddTo(subscriptions); enemy1.OnHitDamage.Subscribe().AddTo(subscriptions); enemy2.OnHitDamage.Subscribe().AddTo(subscriptions); } void OnDestroy() { // subscriptions.Dispose(); }
  26. 26. Curing Your Asynchronous Programming Blues
  27. 27. yield return is awaitable IEnumerator GetBingText() { var www = new WWW("http://bing.com/"); yield return www; // Debug.Log(www.text); } It’s Unity’s awesome feature!
  28. 28. But... IEnumerator GetGoogle() { var www = new WWW("http://google.com/"); yield return www; } IEnumerator OnMouseDown() { try { // yield return StartCoroutine(GetGoogle()); } catch { } } IEnumerator yield return try-catch
  29. 29. IEnumerator GetGoogle(Action<string> onCompleted, Action<Exception> onError) { var www = new WWW("http://google.com/"); yield return www; if (!www.error) onError(new Exception(www.error)); else onCompleted(www.text); } ……(JavaScript ) IEnumerator
  30. 30. Rx Unity ObservableWWW.Get("http://google.co.jp/") .SelectMany(x => ObservableWWW.Get(x)) // .Retry(3) // 3 .Subscribe( x => Debug.Log(x), // ex => Debug.LogException(ex)); // Rx x x x
  31. 31. Why can Rx apply to asynchronous? x IEnumerable<T> IObservable<T> IObservable<T> time event async IE<T>
  32. 32. Why can Rx apply to asynchronous? x IEnumerable<T> IObservable<T> IObservable<T> time event async IE<T> Orchestrate Rx
  33. 33. var parallel = Observable.WhenAll( ObservableWWW.Get("http://google.com/"), ObservableWWW.Get("http://bing.com/"), ObservableWWW.Get("http://unity3d.com/")); parallel.Subscribe(xs => { Debug.Log(xs[0].Substring(0, 100)); // google Debug.Log(xs[1].Substring(0, 100)); // bing Debug.Log(xs[2].Substring(0, 100)); // unity });
  34. 34. IObservable<T> time IObservable<T> time WhenAll Observable.WhenAll( ObservableWWW.Get(), ObservableWWW.Get(), ObservableWWW.Get()) Subscribe(xs => xs[0], xs[1], xs[2])
  35. 35. Conclusion
  36. 36. Reactive Programming UniRx Available Now GitHub - https://github.com/neuecc/UniRx/ Asset Store(FREE) – http://u3d.as/7tT Update (v4.4)

×