-
Notifications
You must be signed in to change notification settings - Fork 117
Observable Examples
rpgwhitelock edited this page Jan 8, 2015
·
11 revisions
Inspired by http://rxwiki.wikidot.com/101samples
Please feel free to add more!
Simple RX Time. Should be self explanatory.
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") });
Once per second, execute command to damage player
Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Subscribe(_ => {
ExecutedamagePerSecondTick();
}).DisposeWhenChanged(Player.healthStateProperty);
Time based functionality without co-routines in controller to trigger waves.
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();
});
}
uGUI events as observables. A button as an event stream in your view.
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
});
Flash sprites on and off rapidly.
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);
}