diff --git a/Assets/ObjectTest/NewBehaviourScript.cs b/Assets/ObjectTest/NewBehaviourScript.cs index e3f85a51..2ec53dc9 100644 --- a/Assets/ObjectTest/NewBehaviourScript.cs +++ b/Assets/ObjectTest/NewBehaviourScript.cs @@ -48,13 +48,13 @@ public override void Start() // DoubleCLick Sample of // The introduction to Reactive Programming you've been missing // https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 - //OnMouseDownAsObservable().Buffer(OnMouseDownAsObservable().Throttle(TimeSpan.FromMilliseconds(250))) - // .Where(xs => - // { - // logger.Debug(xs.Count); - // return xs.Count >= 2; - // }) - // .Subscribe(_ => logger.Debug("Double Click Detected")); + + //var clickStream = Observable.EveryUpdate() + // .Where(_ => Input.GetMouseButtonDown(0)); + + //clickStream.Buffer(clickStream.Throttle(TimeSpan.FromMilliseconds(250))) + // .Where(xs => xs.Count >= 2) + // .Subscribe(xs => Debug.Log("DoubleClick Detected! Count:" + xs.Count)); base.Start(); } diff --git a/Assets/UniRx/Examples/Sample04_ConvertFromUnityCallback.cs b/Assets/UniRx/Examples/Sample04_ConvertFromUnityCallback.cs index 31e84f34..2ca295a0 100644 --- a/Assets/UniRx/Examples/Sample04_ConvertFromUnityCallback.cs +++ b/Assets/UniRx/Examples/Sample04_ConvertFromUnityCallback.cs @@ -4,6 +4,8 @@ namespace UniRx.Examples { public class Sample04_ConvertFromUnityCallback : TypedMonoBehaviour { + // This is about log but more reliable log sample => Sample11_Logger + private class LogCallback { public string Condition; @@ -11,7 +13,7 @@ private class LogCallback public UnityEngine.LogType LogType; } - private static class LogHelper + static class LogHelper { static Subject subject; diff --git a/Assets/UniRx/Examples/Sample06_ConvertToCoroutine.cs b/Assets/UniRx/Examples/Sample06_ConvertToCoroutine.cs index df6a4e74..9d3dbbc1 100644 --- a/Assets/UniRx/Examples/Sample06_ConvertToCoroutine.cs +++ b/Assets/UniRx/Examples/Sample06_ConvertToCoroutine.cs @@ -15,5 +15,16 @@ public class Sample06_ConvertToCoroutine : TypedMonoBehaviour Debug.Log(v); // 10(callback is last value) } + + // like WWW.text/error, LazyTask is awaitable value container + IEnumerator LazyTaskTest() + { + // IObservable + var task = Observable.Start(() => 100).ToLazyTask(); + + yield return task.Start(); // wait for OnCompleted + + Debug.Log(task.Result); // or task.Exception + } } } \ No newline at end of file diff --git a/Assets/UniRx/Examples/Sample08_DetectDoubleClick.cs b/Assets/UniRx/Examples/Sample08_DetectDoubleClick.cs index 49c2189e..30eea175 100644 --- a/Assets/UniRx/Examples/Sample08_DetectDoubleClick.cs +++ b/Assets/UniRx/Examples/Sample08_DetectDoubleClick.cs @@ -17,18 +17,16 @@ public override void Awake() // Observable.EveryApplicationFocus/EveryApplicationPause // Observable.OnceApplicationQuit - var detected = false; - Observable.EveryUpdate() - .Where(_ => Input.GetMouseButtonDown(0)) - .Buffer(TimeSpan.FromMilliseconds(300), TimeSpan.FromMilliseconds(100)) - .Where(xs => xs.Count >= 2 && !detected) - .Do(_ => - { - detected = true; - Scheduler.ThreadPool.Schedule(TimeSpan.FromSeconds(1), () => detected = false); - }) - .ObserveOnMainThread() - .Subscribe(_ => Debug.Log("DoubleClick Detected")); + // This DoubleCLick Sample is from + // The introduction to Reactive Programming you've been missing + // https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 + + var clickStream = Observable.EveryUpdate() + .Where(_ => Input.GetMouseButtonDown(0)); + + clickStream.Buffer(clickStream.Throttle(TimeSpan.FromMilliseconds(250))) + .Where(xs => xs.Count >= 2) + .Subscribe(xs => Debug.Log("DoubleClick Detected! Count:" + xs.Count)); } } } \ No newline at end of file diff --git a/Assets/UniRx/Examples/Sample10_MainThreadDispatcher.cs b/Assets/UniRx/Examples/Sample10_MainThreadDispatcher.cs index 4173d08e..3875184f 100644 --- a/Assets/UniRx/Examples/Sample10_MainThreadDispatcher.cs +++ b/Assets/UniRx/Examples/Sample10_MainThreadDispatcher.cs @@ -22,14 +22,15 @@ public void Run() // Add Action to MainThreadDispatcher. Action is saved queue, run on next update. MainThreadDispatcher.Post(() => Debug.Log("test")); - // Timebased operations is run on Threadpool(as default) - // ObserveOnMainThread return to mainthread + // Timebased operations is run on MainThread(as default) + // All timebased operation(Interval, Timer, Delay, Buffer, etc...)is single thread, thread safe! Observable.Interval(TimeSpan.FromSeconds(1)) - .ObserveOnMainThread() .Subscribe(x => Debug.Log(x)); - // Run on MainThreadScheduler - Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.MainThread) + // Observable.Start use ThreadPool Scheduler as default. + // ObserveOnMainThread return to mainthread + Observable.Start(() => Unit.Default) // asynchronous work + .ObserveOnMainThread() .Subscribe(x => Debug.Log(x)); } diff --git a/Assets/UniRx/ReadMe.txt b/Assets/UniRx/ReadMe.txt index 7f7a0818..c76ca18d 100644 --- a/Assets/UniRx/ReadMe.txt +++ b/Assets/UniRx/ReadMe.txt @@ -1,11 +1,12 @@ -UniRx - Reactive Extensions for Unity / ver.4.4 +UniRx - Reactive Extensions for Unity / ver.4.5 === Created by Yoshifumi Kawai(neuecc) What is UniRx? --- UniRx(Reactive Extensions for Unity) is re-implementation of .NET Reactive Extensions. -It's free and open source on GitHub. +It's free and open source on GitHub. +Supported platforms are PC/Android/iOS/WP8/WindowsStore. You can check latest info, source code and issues on https://github.com/neuecc/UniRx We welcome to your contribute such as bug report, request, and pull request. @@ -32,6 +33,26 @@ Rx considere event as reactive sequence which is possible to compose and perform Unity is single thread but UniRx helps multithreading for join, cancel, access GameObject etc. +The Introduction +--- +Great introduction article of Rx - [The introduction to Reactive Programming you've been missing](https://gist.github.com/staltz/868e7e9bc2a7b8c1f754). Following code is same sample of detect double click. + +``` +var clickStream = Observable.EveryUpdate() + .Where(_ => Input.GetMouseButtonDown(0)); + +clickStream.Buffer(clickStream.Throttle(TimeSpan.FromMilliseconds(250))) + .Where(xs => xs.Count >= 2) + .Subscribe(xs => Debug.Log("DoubleClick Detected! Count:" + xs.Count)); +``` + +This example includes the following contents(In only five lines!). + +* Game loop(Update) as event stream +* Event stream is composable +* Merging self stream +* Easily handle time based operation + How to Use for WWW --- async operation, use ObservableWWW, it's Get/Post returns IObservable. @@ -237,6 +258,16 @@ Observable.WhenAll(heavyMethod, heavyMethod2) }); ``` +DefaultScheduler +--- +UniRx's default time based operation(Interval, Timer, Buffer(timeSpan), etc...)'s Scheduler is Scheduler.MainThread. +It means most operator(excpet Observable.Start) is work on single-thread, +you don't need ObserverOn and you don't mind thread safety. +It's differece with RxNet but better fit to Unity environment. + +Scheduler.MainThread under Time.timeScale's influence. +If you want to ignore, use Scheduler.MainThreadIgnoreTimeScale. + How to Use for MonoBehaviour --- UniRx has two extended MonoBehaviour. TypedMonoBehaviour is typesafe MonoBehaviour. diff --git a/README.md b/README.md index e82efbac..967a75a9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Created by Yoshifumi Kawai(neuecc) What is UniRx? --- -UniRx(Reactive Extensions for Unity) is re-implementation of .NET Reactive Extensions. Official Rx is great but can't work on Unity and has some issue of iOS AOT. This library remove there issues and add some specified utility for Unity. +UniRx(Reactive Extensions for Unity) is re-implementation of .NET Reactive Extensions. Official Rx is great but can't work on Unity and has some issue of iOS AOT. This library remove there issues and add some specified utility for Unity. Supported platforms are PC/Android/iOS/WP8/WindowsStore. UniRx is available in Unity Asset Store(FREE) - http://u3d.as/content/neuecc/uni-rx-reactive-extensions-for-unity/7tT @@ -30,6 +30,26 @@ GameLoop(every Update, OnCollisionEnter, etc), Sensor(like Kinect, Leap Motion, Unity is single thread but UniRx helps multithreading for join, cancel, access GameObject etc. +The Introduction +--- +Great introduction article of Rx - [The introduction to Reactive Programming you've been missing](https://gist.github.com/staltz/868e7e9bc2a7b8c1f754). Following code is same sample of detect double click. + +``` +var clickStream = Observable.EveryUpdate() + .Where(_ => Input.GetMouseButtonDown(0)); + +clickStream.Buffer(clickStream.Throttle(TimeSpan.FromMilliseconds(250))) + .Where(xs => xs.Count >= 2) + .Subscribe(xs => Debug.Log("DoubleClick Detected! Count:" + xs.Count)); +``` + +This example includes the following contents(In only five lines!). + +* Game loop(Update) as event stream +* Event stream is composable +* Merging self stream +* Easily handle time based operation + How to Use for WWW --- async operation, use ObservableWWW, it's Get/Post returns IObservable. @@ -235,6 +255,13 @@ Observable.WhenAll(heavyMethod, heavyMethod2) }); ``` +DefaultScheduler +--- +UniRx's default time based operation(Interval, Timer, Buffer(timeSpan), etc...)'s Scheduler is `Scheduler.MainThread`.It means most operator(excpet `Observable.Start`) is work on single-thread, you don't need ObserverOn and you don't mind thread safety. It's differece with RxNet but better fit to Unity environment. + +`Scheduler.MainThread` under Time.timeScale's influence.If you want to ignore, use ` Scheduler.MainThreadIgnoreTimeScale`. + + How to Use for MonoBehaviour --- UniRx has two extended MonoBehaviour. TypedMonoBehaviour is typesafe MonoBehaviour. diff --git a/UnityVS.UniRx.CSharp.csproj b/UnityVS.UniRx.CSharp.csproj index c4555edf..df2a57f5 100644 --- a/UnityVS.UniRx.CSharp.csproj +++ b/UnityVS.UniRx.CSharp.csproj @@ -7,16 +7,14 @@ 2.0 {55DB241B-2717-422C-3093-18D20F58B73B} Library - - + Assembly-CSharp 512 {E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} .NETFramework v3.5 Unity Subset v3.5 - - + pdbonly @@ -24,7 +22,7 @@ Temp\UnityVS_bin\Debug\ prompt 4 - DEBUG;TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;DEVELOPMENT_BUILD;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN;UNITY_PRO_LICENSE + DEBUG;TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;DEVELOPMENT_BUILD;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN pdbonly @@ -32,7 +30,7 @@ Temp\UnityVS_bin\Release\ prompt 4 - TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;DEVELOPMENT_BUILD;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN;UNITY_PRO_LICENSE + TRACE;UNITY_4_5_1;UNITY_4_5;UNITY_STANDALONE_WIN;ENABLE_MICROPHONE;ENABLE_TEXTUREID_MAP;ENABLE_UNITYEVENTS;ENABLE_NEW_HIERARCHY ;ENABLE_AUDIO_FMOD;UNITY_STANDALONE;ENABLE_MONO;ENABLE_TERRAIN;ENABLE_SUBSTANCE;ENABLE_GENERICS;INCLUDE_WP8SUPPORT;ENABLE_MOVIES;ENABLE_WWW;ENABLE_IMAGEEFFECTS;ENABLE_WEBCAM;INCLUDE_METROSUPPORT;RENDER_SOFTWARE_CURSOR;ENABLE_NETWORK;ENABLE_PHYSICS;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_2D_PHYSICS;ENABLE_SHADOWS;ENABLE_AUDIO;ENABLE_NAVMESH_CARVING;ENABLE_DUCK_TYPING;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;DEVELOPMENT_BUILD;ENABLE_PROFILER;UNITY_EDITOR;UNITY_EDITOR_WIN @@ -133,4 +131,4 @@ - \ No newline at end of file +