123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #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<MyEventArgs> FooBar;
- public event Action<int> FooFoo;
- CompositeDisposable disposables = new CompositeDisposable();
-
-
-
- Subject<int> onBarBar = new Subject<int>();
- public IObservable<int> OnBarBar { get { return onBarBar; } }
- void Start()
- {
-
- Observable.FromEventPattern<EventHandler<MyEventArgs>, MyEventArgs>(
- h => h.Invoke, h => FooBar += h, h => FooBar -= h)
- .Subscribe()
- .AddTo(disposables);
-
- Observable.FromEvent<EventHandler<MyEventArgs>, MyEventArgs>(
- h => (sender, e) => h(e), h => FooBar += h, h => FooBar -= h)
- .Subscribe()
- .AddTo(disposables);
-
- Observable.FromEvent<int>(
- h => FooFoo += h, h => FooFoo -= h)
- .Subscribe()
- .AddTo(disposables);
-
- var capture = 0;
- Observable.FromEventPattern<EventHandler<MyEventArgs>, MyEventArgs>(h =>
- {
- capture.GetHashCode();
- return new EventHandler<MyEventArgs>(h);
- }, h => FooBar += h, h => FooBar -= h)
- .Subscribe()
- .AddTo(disposables);
-
- OnBarBar.Subscribe().AddTo(disposables);
- onBarBar.OnNext(1);
- }
- void OnDestroy()
- {
-
- disposables.Dispose();
- }
- }
- }
- #pragma warning restore 0067
|