見出し画像

RAYSER進捗(20231010)

引き続きMessagePipeを調べながら、ソースを記述しています。
サブウェポン選択については、構造体で定義した情報を送受信できるようにする想定です。

using _RAYSER.Scripts.Item;
using MessagePipe;
using UnityEngine;
using VContainer;
using VContainer.Unity;

namespace _RAYSER.Scripts.Weapon
{
    /// <summary>
    /// サブウェポン用LifetimeScope
    /// </summary>
    public class SubWeaponLifetimeScope : LifetimeScope
    {
        [SerializeField] private ItemButton[] itemButtons = default;
        protected override void Configure(IContainerBuilder builder)
        {
            // MessagePipeの設定
            var options = builder.RegisterMessagePipe();
            builder.RegisterMessageBroker<CurrentSubWeaponIndex>(options);

            if (itemButtons != null)
            {
                foreach (var button in itemButtons)
                {
                    builder.RegisterInstance(button).AsImplementedInterfaces();
                }
            }
        }
    }
}
namespace _RAYSER.Scripts.Weapon
{
    /// <summary>
    /// 選択中のサブウェポンのインデックス
    /// </summary>
    public struct CurrentSubWeaponIndex
    {
        public int currentSubWeaponIndex { get; } = 0

        public CurrentSubWeaponIndex(int currentSubWeaponIndex)
        {
            this.currentSubWeaponIndex = currentSubWeaponIndex;
        }
    }
}

UI側のMessagePipeの記述は調べている途中のため、一旦コメントアウトしています。

using System;
using UnityEngine;

namespace _RAYSER.Scripts.Weapon
{
    /// <summary>
    /// サブウェポンUI
    /// </summary>
    public class SubWeaponUI : MonoBehaviour
    {
        /// <summary>
        /// 対象武器のインデックス
        /// </summary>
        [SerializeField] private int weaponIndex;

        private void Start()
        {
            // MessagePipe受信処理
            // this.Subscribe<CurrentSubWeaponIndex>(x =>
            // {
            //     if (x.currentSubWeaponIndex == weaponIndex)
            //     {
            //         // 選択中のサブウェポン
            //         Debug.Log("選択中のサブウェポン");
            //     }
            // }
        }
    }
}

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