#pragma warning disable 0067 using System; using UnityEngine; namespace UniRx.Examples { public class Sample09_EventHandling : MonoBehaviour { public class MyEventArgs : EventArgs { public int MyProperty { get; set; } } public event EventHandler FooBar; public event Action FooFoo; CompositeDisposable disposables = new CompositeDisposable(); // Subject is Rx's native event expression and recommend way for use Rx as event. // Subject.OnNext as fire event, // expose IObserver is subscibable for external source, it's no need convert. Subject onBarBar = new Subject(); public IObservable OnBarBar { get { return onBarBar; } } void Start() { // convert to IO as (sender, eventArgs) Observable.FromEventPattern, MyEventArgs>( h => h.Invoke, h => FooBar += h, h => FooBar -= h) .Subscribe() .AddTo(disposables); // IDisposable can add to collection easily by AddTo // convert to IO, many situation this is useful than FromEventPattern Observable.FromEvent, MyEventArgs>( h => (sender, e) => h(e), h => FooBar += h, h => FooBar -= h) .Subscribe() .AddTo(disposables); // You can convert Action like event. Observable.FromEvent( h => FooFoo += h, h => FooFoo -= h) .Subscribe() .AddTo(disposables); // AOT Safe EventHandling, use dummy capture, see:https://github.com/neuecc/UniRx/wiki/AOT-Exception-Patterns-and-Hacks var capture = 0; Observable.FromEventPattern, MyEventArgs>(h => { capture.GetHashCode(); // dummy for AOT return new EventHandler(h); }, h => FooBar += h, h => FooBar -= h) .Subscribe() .AddTo(disposables); // Subject as like event. OnBarBar.Subscribe().AddTo(disposables); onBarBar.OnNext(1); // fire event } void OnDestroy() { // manage subscription lifecycle disposables.Dispose(); } } } #pragma warning restore 0067