1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

非同期処理2022/UniRxの使い道

Last updated at Posted at 2022-07-23

参考 2022年現在におけるUniRxの使いみち 

非同期処理の手法

 → async/awaitを使え。

async/await、Task 「なにかの処理を1回だけ待つ」
コルーチン
UniRx... イベント処理が強い、繰り返し実行することに強い。
UniTask(Unity)

UniRx

1.イベント通知,入力イベントなど。
2.MVPパターンに使う
3.非同期処理はもう非推奨
4.MonoBehaviourのロジックを記述する

処理したあとN秒間はクールタイム。
わかりやすく言い換えるなら、「強攻撃を出したらクールタイムが長い。弱攻撃を出したらクールタイムが短い。クールタイム中は攻撃が一切できない」みたいなパターンです。

        private void Start()
        {
            // 毎フレーム実行
            this.UpdateAsObservable()
                // 攻撃ボタンが押されたら
                .Where(_ => Input.GetButtonDown("Attack"))
                // 処理を1回走らせたあと、1秒間クールタイム
                .ThrottleFirst(TimeSpan.FromSeconds(1))
                .Subscribe(_ => Action())
                .AddTo(this);
        }

5.Update()のスコープを分離する

        private void Start()
        {
            // Playerの位置に応じた処理
            this.UpdateAsObservable()
                .Subscribe(_ => CheckPosition())
                .AddTo(this);

            // Playerの体力に応じた処理
            this.UpdateAsObservable()
                .Subscribe(_ => CheckHealth())
                .AddTo(this);

            // 攻撃処理のチェックと移動処理
            this.UpdateAsObservable()
                .Subscribe(_ =>
                {
                    CheckAttack();
                    Move();
                })
                .AddTo(this);
        }

6.依存関係の整理に使う

7.スクリプトの実行順の調整に使う
「前のコンポーネントが終わったら次のコンポーネントが連鎖して実行される」という仕組みにします。

8.コレクションの変動を通知する
「配列や辞書の中身が変動した」ということを即時に検知してイベント処理を実行することが可能となります。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?