UniRxで、uGUIのボタンを長押しするとエネルギーを溜めて、ボタンを離すと発射という処理にチャレンジ
UniRxを始めてみました。
uGUIのボタンを押し始めたらエネルギーチャージが始まり、押してる時間によって三段階のパワーがあり、
ボタンを離したら発射するみたいな処理をUniRxを使ってやってみようとチャレンジしてみました。
今回使用したバージョン
- Unity 5.4.1
- UniRx 5.4.1.1
using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using System.Linq; using System; using UniRx; using UniRx.Triggers; using Unity.Linq; ... Button chargeButton; ... void Start () { chargeButton.UpdateAsObservable() .SkipUntil(chargeButton.OnPointerDownAsObservable()) .TakeUntil(chargeButton.OnPointerUpAsObservable().Do(_ => { // ボタンを離したときの処理 })) .Select(_ => 1) .Scan((sum, addCount) => { return sum + addCount; }) .Repeat() .Subscribe(totalCount => { if(totalCount == 1) // 0だと発生しない { // 1フレーム目での処理 } if(totalCount == 60) { // 60フレーム目での処理 } if(totalCount == 120) { // 120フレーム目での処理 } }); }
こんな形であっているのだろうか・・・