見出し画像

RAYSER進捗(20230929)

RAYSERの進捗です、VContainerを使ったスコア表示ができるようになりました。Licenseボタンに加算処理を入れてテストしてます。

using UnityEngine;
using VContainer;
using VContainer.Unity;

namespace Score
{
    /// <summary>
    /// タイトルのスコアのライフタイムスコープ
    /// タイトルシーンのGameObjectにアタッチして使用する
    /// </summary>
    public class ScoreSceneTitleLifetimeScope : LifetimeScope
    {
        [SerializeField] private ScoreScreen _scoreScreen;

        protected override void Configure(IContainerBuilder builder)
        {
            base.Configure(builder);

            builder.Register<ScoreService>(Lifetime.Singleton);
            builder.Register<ScoreDataPresenter>(Lifetime.Singleton);
            builder.RegisterEntryPoint<ScoreDataPresenter>();
            builder.RegisterComponent(_scoreScreen);
        }
    }
}
using VContainer;

namespace Score
{
    public class ScoreService
    {
        private ScoreData scoreData;
        private ScoreScreen scoreScreen;

        [Inject]
        public void Construct(ScoreData scoreData, ScoreScreen scoreScreen)
        {
            this.scoreData = scoreData;
            this.scoreScreen = scoreScreen;
        }

        /// <summary>
        /// スコア加算処理
        /// </summary>
        /// <param name="score">スコア</param>
        public void AddScore(int score)
        {
            scoreData.SetScore(scoreData.GetScore() + score);
        }

        /// <summary>
        /// スコア表示処理
        /// </summary>
        public void ShowScore()
        {
            scoreScreen.ShowScore(scoreData.GetScore());
        }
    }
}

UniRxを用いたAddtoの処理の部分はかめふぃさんと山々田さんからの助言で公式ドキュメントと参考用のGithubの情報をいただき、それらの情報を参考にDisposeで実装してみました。情報ありがとうございました!

using System;
using Event.Signal;
using UniRx;
using VContainer.Unity;

namespace Score
{
    public class ScoreDataPresenter : IStartable, IDisposable
    {
        readonly ScoreService scoreService;
        readonly CompositeDisposable disposable = new CompositeDisposable();

        public ScoreDataPresenter(ScoreService scoreService)
        {
            this.scoreService = scoreService;
        }

        void IStartable.Start()
        {
            MessageBroker.Default.Receive<ScoreAccumulation>()
                .Subscribe(x => this.RefreshUI(x.Score))
                .AddTo(disposable);
        }

        private void RefreshUI(int score)
        {
            scoreService.AddScore(score);
            scoreService.ShowScore();
        }

        public void Dispose()
        {
            disposable.Dispose();
        }
    }
}
using TMPro;
using UnityEngine;

namespace Score
{
    /// <summary>
    /// スコアのViewクラス
    /// </summary>
    public class ScoreScreen : MonoBehaviour
    {
        [SerializeField] private TextMeshProUGUI scoreUI;

        public void ShowScore(int score)
        {
            scoreUI.text = score.ToString();
        }
    }
}

ScoreSceneTitleLifetimeScopeを適当なGameObjectにアタッチ

ScoreSceneTitleLifetimeScope

ScoreScreenを表示したいTextMeshProのオブジェクトにアタッチして、TextMeshProUGUIを定義しています。

View部分


この記事が気に入ったらサポートをしてみませんか?