Unity_Rx
(example)
Observable
View(Mono)
(position)
protected
override
Vector3
CalculatevPos (){ ... }
return this.transform.position;
// calculates on each update
IObservable<Vector3>
GetvPosObservable (){ ... }
return base.GetvPosObservable ();
(overrides)
return PositionAsObservable.Select(p=>CalculatevPos());
//only invokes if postion has changed
//p==Vector3 (pos) ???
// or p == p<T> class (property Class for viewmodel )
// Viewmodel collection = ModelCollection<T> class
return PositionAsObservable.Sample(TimeSpan.FromSeconds(1.0f)).Select(p=>CalculatevPos());
//only invokes every x seconds
(Update)
this.UpdateAsObservable().Subscribe(_=>{ Debug.Log("THIS IS CALLED EVERY FRAME") });
this.UpdateAsObservable().Where(_=>Input.GetMouseButtonDown(0)).Subscribe(_=>{ Debug.Log("THIS IS CALLED when the mouse button is down") });
this.UpdateAsObservable().Where(_=>Input.GetMouseButtonDown(0)).Throttle(TimeSpan.FromSeconds(1)).Subscribe(_=>{ Debug.Log("This is only called once person second. This keeps many clicks from firing this to quickly") });
(uGUI)
Button poorButton; // Our button.
poorButton.AsClickObservable()
.Where(_ => YOUR FILTER )
.Throttle(_ => PREVENT SPAMMING )
.DelayFrame( FRAMES TO DELAY )
.SkipWhile( SKIP FIRST TAPS IF CONDITION IS NOT MET )
.Subscribe(_ =>
{
USE LIKE SIMPLE OBSERVABLE
});
//uGUI events as observables. A button as an event stream in your view.
(sprites)
var sprites = GetComponentsInChildren().ToList ();
if(value){
Observable.Interval(TimeSpan.FromMilliseconds(100))
.Subscribe(l =>
{
if (Character.isInvulnerable)
sprites.ForEach(s => s.enabled = 1 % 2 == 0);
}).DisposeWhenChanged(Character._isInvulnerableProperty);
}
else
{
sprites.ForEach(s => s.enabled = true);
}
//Flash sprites on and off rapidly.
(statemachine)
{StateMachineProperty}Property.Subscribe(_=>{ {StateMachineProperty}Property.LastState });
//Bind to state machines last state
Timer
Observable.Timer(TimeSpan.FromMilliseconds(100)).Subscribe(...);
l=>{ myElement.JumpLock = false; }
//l=> ; l==long???
Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Subscribe(_ => {
ExecutedamagePerSecondTick();
}).DisposeWhenChanged(Player.healthStateProperty);
//Once per second, execute command to damage player
if (state is Wave)
{
// Wait for the alloted amount of time
Observable.Interval(TimeSpan.FromSeconds(wavesFPSGame.SpawnWaitSeconds))
.Where(_ => wavesFPSGame.WavesState is Wave)
.Take(wavesFPSGame.KillsToNextWave)
.Subscribe(l => {
SpawnEnemy();
});
}
//Time based functionality without co-routines in controller to trigger waves.
(Transform)
??? What is the difference between
Subscribe
Bind
(def)
LINQ
// allows you query data at rest
Rx
// Rx offers the ability to query data in motion
public
interface
IObservable<out T>{ ... }
IObservable<T>
// 'write/publisher' interface
// streaming sequence of object T
// not == ioStream; but has streaming(push), disposing(closing) and completing(eof)
// Rx also extends the metaphor by introducing concurrency constructs, and query operations like transformation, merging, aggregating and expanding
// IObservable<T> instances are sequences of data in motion
// While instances of IEnumerable<T> are also sequences, we will adopt the convention that they are sequences of data at rest, and
IDisposable
Subscribe(IObserver<T> observer);
//only link to subscription, if not captured is lost...can't gc...memory leak
//built in methods gc???
IObserver<in T>{ ... }
IObserver
// 'reader/consumer' interface
void
OnNext(T value);
//Provides the observer with new data.
OnError(Exception error);
//Notifies the observer that the provider has experienced an error condition.
OnCompleted();
//Notifies the observer that the provider has finished sending push-based notifications.