橋本翼(ツバサムス)

メタバースプログラマー。
UnityとUnreal Engineを専門的に扱う。
メタバース系スタートアップ企業に所属。

詳細はこちら

【Unity】ReactivePropertyの実装例(MVPパターン)

Unity

ReactivePropertyの実装例(MVPパターン)

サンプルゲーム概要

  • 左クリックする度にカウントが更新されていく
  • カウントは0からスタート
  • UniRxのReactivePropertyを使用(MVPパターン
「MVPパターン」とは、「P」にあたるPresenterのみが、「M」であるModelと「V」であるViewへの参照を持つ形のことで、各クラスの疎結合化を目指す。
サンプルゲーム画面

スクリプト

GameData.cs(Model)

using UniRx;//UniRxを使用
using UnityEngine;

public class GameData : MonoBehaviour
{
    //int型のReactivePropertyを作成
    public ReactiveProperty<int> Count = new();

    void Reset()
    {
        //ReactivePropertyに初期値を代入
        Count = new(0);
    }
}

GameManager.cs(Presenter)

~AsObservableメソッドなし
using UniRx;//UniRxを使用
using UnityEngine;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    private UIManager uiManager;//View

    [SerializeField]
    private GameData gameData;//Model

    private void Start()
    {
        //GameDataクラス(Model)のReactivePropertyのCountを
        gameData.Count
            //講読(監視)し、
            //int型のx変数(gameData.Count.Value)を渡す
            .Subscribe(x 
                //UIManagerクラス(View)の
                //UpdateDisplayCountメソッドを呼び出し、
                //引数にx変数の値を渡し、
                //表示を更新する
                => uiManager.UpdateDisplayCount(x))
            //GameManagerクラス(Presenter)が消えたら講読をやめる
            .AddTo(this);
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))gameData.Count.Value++;
    }
}
~AsObservableメソッドあり
using UniRx;//UniRxを使用
using UniRx.Triggers;//~AsObservableメソッドを使用
using UnityEngine;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    private UIManager uiManager;//View

    [SerializeField]
    private GameData gameData;//Model

    private void Start()
    {
        //GameDataクラス(Model)のReactivePropertyのCountを
        gameData.Count
            //講読(監視)し、
            //int型のx変数(gameData.Count.Value)を渡す
            .Subscribe(x
                //UIManagerクラス(View)の
                //UpdateDisplayCountメソッドを呼び出し、
                //引数にx変数の値を渡し、
                //表示を更新する
                => uiManager.UpdateDisplayCount(x))
            //GameManagerクラス(Presenter)が消えたら講読をやめる
            .AddTo(this);

        //GameManagerクラス(Presenter)のUpdateメソッドを
        //Observableに変換して購読する(監視開始)
        this.UpdateAsObservable()
            //Observableからメッセージが
            //毎フレーム発行されるが、
            //左クリックされていない間は
            //それらのメッセージを無視する
            .Where(_ => Input.GetMouseButtonDown(0))
            //Observableから発行されたメッセージを受け取って
            .Subscribe(_
                //GameDataクラス(Model)の
                //ReactivePropertyのCountの
                //Valueを1増やす
                => gameData.Count.Value++)
            //GameManagerクラス(Presenter)が消えたら講読をやめる
            .AddTo(this);
    }
}

UIManager.cs(View)

using UnityEngine.UI;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    [SerializeField]
    private Text txtCount;

    public void UpdateDisplayCount(int count)//引数を利用
    {
        txtCount.text = count.ToString();
    }
}

お問い合わせ

    コメント

    タイトルとURLをコピーしました