### Unity TimeProvider and FrameProvider Examples Source: https://github.com/cysharp/r3/blob/main/README.md Examples demonstrating the usage of R3's Unity-specific TimeProvider and FrameProvider for reactive operations within Unity's player loop. ```csharp // ignore-timescale based interval Observable.Interval(TimeSpan.FromSeconds(5), UnityTimeProvider.UpdateIgnoreTimeScale); // fixed-update loop Observable.EveryUpdate(UnityFrameProvider.FixedUpdate); // observe PostLateUpdate Observable.Return(42).ObserveOn(UnityFrameProvider.PostLateUpdate); ``` -------------------------------- ### Basic Observable Interval Example Source: https://github.com/cysharp/r3/blob/main/README.md Demonstrates creating an observable that emits sequential numbers at a specified interval, filtering even numbers, and subscribing to the output. This example requires importing the R3 namespace. ```csharp using R3; var subscription = Observable.Interval(TimeSpan.FromSeconds(1)) .Select((_, i) => i) .Where(x => x % 2 == 0) .Subscribe(x => Console.WriteLine($"Interval:{x}")); var cts = new CancellationTokenSource(); ``` -------------------------------- ### Install R3 NuGet Package Source: https://github.com/cysharp/r3/blob/main/README.md Install the R3 library using the .NET CLI. This package supports various .NET versions and platforms. ```bash dotnet add package R3 ``` -------------------------------- ### ObservableTracker State Examples Source: https://github.com/cysharp/r3/blob/main/README.md Examples of the TrackingState objects that ObservableTracker provides. ```csharp TrackingState { TrackingId = 1, FormattedType = Timer._Timer, AddTime = 2024/01/09 4:11:39, StackTrace =... TrackingState { TrackingId = 2, FormattedType = Where`1._Where, AddTime = 2024/01/09 4:11:39, StackTrace =... TrackingState { TrackingId = 3, FormattedType = Take`1._Take, AddTime = 2024/01/09 4:11:39, StackTrace =... ``` -------------------------------- ### Basic Timer Example Source: https://github.com/cysharp/r3/blob/main/README.md Demonstrates a simple timer that runs every 3 seconds and cancels after a key press. ```csharp _ = Task.Run(() => { Console.ReadLine(); cts.Cancel(); }); await Observable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3)) .TakeUntil(cts.Token) .ForEachAsync(x => Console.WriteLine($"Timer")); subscription.Dispose(); ``` -------------------------------- ### R3 Range Subscription Example Source: https://github.com/cysharp/r3/blob/main/README.md Demonstrates subscribing to an Observable.Range in R3, which is treated as immediate execution. ```csharp `Observable.Range(1, 10000).Subscribe()` ``` -------------------------------- ### Image Asset Naming Conventions Source: https://github.com/cysharp/r3/blob/main/sandbox/UnoSampleApp/UnoSampleApp/Assets/SharedAssets.md Examples of how to name image assets for different scales in a shared project. Ensure the build action is set to 'Content'. ```text \Assets\Images\logo.scale-100.png \Assets\Images\logo.scale-200.png \Assets\Images\logo.scale-400.png \Assets\Images\scale-100\logo.png \Assets\Images\scale-200\logo.png \Assets\Images\scale-400\logo.png ``` -------------------------------- ### Asynchronous Producer-Consumer Example Source: https://github.com/cysharp/r3/blob/main/src/R3.Unity/Assets/Packages/System.Threading.Channels.8.0.0/PACKAGE.md Demonstrates a basic producer-consumer pattern using an unbounded channel. The producer writes integers to the channel every second, and the consumer reads and prints them asynchronously. Ensure `System.Threading.Channels` is referenced. ```C# using System; using System.Threading.Channels; using System.Threading.Tasks; Channel channel = Channel.CreateUnbounded(); Task producer = Task.Run(async () => { int i = 0; while (true) { channel.Writer.TryWrite(i++); await Task.Delay(TimeSpan.FromSeconds(1)); } }); Task consumer = Task.Run(async () => { await foreach (int value in channel.Reader.ReadAllAsync()) { Console.WriteLine(value); } }); await Task.WhenAll(producer, consumer); ``` -------------------------------- ### Install R3 Unity Package via Git URL Source: https://github.com/cysharp/r3/blob/main/README.md Reference the R3.Unity package using its Git URL. You can specify a version tag for stability. ```bash https://github.com/Cysharp/R3.git?path=src/R3.Unity/Assets/R3.Unity https://github.com/Cysharp/R3.git?path=src/R3.Unity/Assets/R3.Unity#1.0.0 ``` -------------------------------- ### WPF UI Property Binding Example Source: https://github.com/cysharp/r3/blob/main/README.md This example demonstrates binding UI element properties (Width, Height) to ViewModel properties using `Observable.EveryValueChanged` in a WPF `MainWindow`. Remember to dispose of subscriptions on window close. ```csharp public partial class MainWindow : Window { IDisposable disposable; public MainWindow() { InitializeComponent(); var d1 = Observable.EveryValueChanged(this, x => x.Width).Subscribe(x => WidthText.Text = x.ToString()); var d2 = Observable.EveryValueChanged(this, x => x.Height).Subscribe(x => HeightText.Text = x.ToString()); disposable = Disposable.Combine(d1, d2); } protected override void OnClosed(EventArgs e) { disposable.Dispose(); } } ``` -------------------------------- ### Take Source: https://github.com/cysharp/r3/blob/main/docs/reference_operator.md Returns a specified number of contiguous elements from the start of an observable sequence or a time duration. ```APIDOC ## Take ### Description Returns a specified number of contiguous elements from the start of an observable sequence or a time duration. ### Method Extension Method ### Signature `Observable Take(this Observable source, Int32 count)` `Observable Take(this Observable source, TimeSpan duration)` `Observable Take(this Observable source, TimeSpan duration, TimeProvider timeProvider)` ### Returns An `Observable` that contains the elements taken from the start of the source sequence. ``` -------------------------------- ### Reactive Notification Model Example Source: https://github.com/cysharp/r3/blob/main/README.md Illustrates creating an observable model using ReactiveProperty for HP and dead status. HP changes trigger updates and conditional subscriptions. ```csharp // Reactive Notification Model public class Enemy { public ReactiveProperty CurrentHp { get; private set; } public ReactiveProperty IsDead { get; private set; } public Enemy(int initialHp) { // Declarative Property CurrentHp = new ReactiveProperty(initialHp); IsDead = CurrentHp.Select(x => x <= 0).ToReactiveProperty(); } } // --- // Click button, HP decrement MyButton.OnClickAsObservable().Subscribe(_ => enemy.CurrentHp.Value -= 99); // subscribe from notification model. enemy.CurrentHp.Subscribe(x => Console.WriteLine("HP:" + x)); enemy.IsDead.Where(isDead => isDead == true) .Subscribe(_ => { // when dead, disable button MyButton.SetDisable(); }); ``` -------------------------------- ### Immediate Emission Example Source: https://github.com/cysharp/r3/blob/main/README.md Demonstrates how Observable.Range can emit values immediately, making it difficult to stop the stream even after disposal. This behavior is due to the nature of immediate value emission. ```csharp Observable.Range(0, int.MaxValue) .Do(onNext: x => Console.WriteLine($"Do:{x}")) .Take(10) .Subscribe(x => Console.WriteLine($"Subscribe:{x}")); ``` -------------------------------- ### MAUI Default Exception Handler Implementation Source: https://github.com/cysharp/r3/blob/main/README.md Example implementation of `R3MauiDefaultExceptionHandler` for MAUI, which logs unhandled exceptions using `System.Diagnostics.Trace` and `ILogger`. ```csharp public class R3MauiDefaultExceptionHandler(IServiceProvider serviceProvider) : IR3MauiExceptionHandler { public void HandleException(Exception ex) { System.Diagnostics.Trace.TraceError("R3 Unhandled Exception {0}", ex); var logger = serviceProvider.GetService>(); logger?.LogError(ex, "R3 Unhandled Exception"); } } ``` -------------------------------- ### ReactiveCommand Example in WPF Source: https://github.com/cysharp/r3/blob/main/README.md Demonstrates the usage of ReactiveCommand within a WPF ViewModel and its binding in XAML. The command is triggered by a CheckBox's state. ```csharp public class CommandViewModel : IDisposable { public BindableReactiveProperty OnCheck { get; } // bind to CheckBox public ReactiveCommand ShowMessageBox { get; } // bind to Button, non generics ReactiveCommand is ReactiveCommand public CommandViewModel() { OnCheck = new BindableReactiveProperty(); ShowMessageBox = OnCheck.ToReactiveCommand(_ => { MessageBox.Show("clicked"); }); } public void Dispose() { Disposable.Dispose(OnCheck, ShowMessageBox); } } ``` ```xml