見出し画像

RAYSER進捗(20231002)

RAYSERの進捗ですが、カスタマイズ画面のボタンとウィンドウを作るなどしていました。まだ中の要素をどのようにするか模索中です。


ウィンドウとボタンは以前作ったものとほぼ同等です。これとは別件でウィンドウをリサイズした時にUIがずれる現象があり、それの対策もしないといけないと考え中です。アンカーの設定などはしていますが、スクリプトで初期のポジション値を入れている処理がもしかするとよくないのかなと考えています。(見えないUIを重ねたことで、ボタンが押せなくなったりしたことがあり、使わないUIのポジションをずらしたりして対応していますが、その方法がそもそも間違いのような気がしています。)

using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;

namespace _RAYSER.Scripts.UI.Title
{
    public class CustomizeWindowUI : MonoBehaviour, IWindowUI
    {
        UIActiveSetter _uiActiveSetter = new UIActiveSetter();
        private IWindowUI _iuiImplementation;

        UIEffect _uiEffect = new UIEffect();
        private IUIEffect _ieffectImplementation;

        private Vector3 _initialPosition = Vector3.zero;
        private Vector2 _initialUISizeDelta = Vector2.zero;

        private CancellationTokenSource cts = new CancellationTokenSource();

        private float setDeltaMinx = 0;
        private float setDeltaMinY = 2f;
        private float setDeltaDuration = 1f;

        [SerializeField] private CanvasGroup windowCanvasGroup;
        [SerializeField] private RectTransform windowRectTransform;
        [SerializeField] private CanvasGroup headerImageCanvasGroup;
        [SerializeField] private CanvasGroup insideButtonsCanvasGroup;
        [SerializeField] private CanvasGroup contentCanvasGroup;

        /// <summary>
        /// 最初にフォーカスになるライセンスUIのボタン
        /// </summary>
        [SerializeField] private GameObject firstFocusUI;

        public UIActiveSetter UIActiveSetter
        {
            get => _iuiImplementation.UIActiveSetter;
            set => _iuiImplementation.UIActiveSetter = value;
        }

        /// <summary>
        /// キャンセルトークンにキャンセル要求を発行する
        /// </summary>
        private void Cancel()
        {
            cts.Cancel();
        }

        private void Awake()
        {
            _initialPosition = windowRectTransform.position;
            _initialUISizeDelta = windowRectTransform.sizeDelta;

            InitializeUI();

            // UI無効
            SetActive(false);
        }

        public void SetActive(bool isActive)
        {
            _uiActiveSetter.SetActive(gameObject, isActive);
        }

        private void InitializeUI()
        {
            _uiEffect.SetAlphaZero(windowCanvasGroup);
            _uiEffect.SetSizeDeltaZero(windowRectTransform);
            _uiEffect.SetAlphaZero(headerImageCanvasGroup);
            _uiEffect.SetAlphaZero(insideButtonsCanvasGroup);
            _uiEffect.SetAlphaZero(contentCanvasGroup);
        }

        public async UniTask ShowUI()
        {
            try
            {
                SetActive(true);
                InitializeUI();

                // ライセンスUI表示
                await _uiEffect.FadeIn(windowCanvasGroup, cts.Token);
                await _uiEffect.SizeDelta(windowRectTransform, new Vector2(_initialUISizeDelta.x, setDeltaMinY),
                    setDeltaDuration, cts.Token);
                await _uiEffect.SizeDelta(windowRectTransform,
                    new Vector2(_initialUISizeDelta.x, _initialUISizeDelta.y), setDeltaDuration, cts.Token);

                // 見出しイメージ表示
                await _uiEffect.FadeIn(headerImageCanvasGroup, cts.Token);

                // テキスト表示
                await _uiEffect.FadeIn(contentCanvasGroup, cts.Token);

                // UI内ボタン表示
                await _uiEffect.FadeIn(insideButtonsCanvasGroup, cts.Token);

                //初期選択ボタンの再指定
                EventSystem.current.SetSelectedGameObject(firstFocusUI);
            }
            catch (OperationCanceledException)
            {
                // キャンセルされた場合の処理
                Debug.Log("FadeIn Canceled");
            }
        }

        public async UniTask HideUI()
        {
            try
            {
                //初期選択ボタンの初期化
                EventSystem.current.SetSelectedGameObject(null);

                // 見出しイメージ非表示
                await _uiEffect.FadeOut(headerImageCanvasGroup, cts.Token);

                // テキスト非表示
                await _uiEffect.FadeOut(contentCanvasGroup, cts.Token);

                // UI内ボタン非表示
                await _uiEffect.FadeOut(insideButtonsCanvasGroup, cts.Token);

                // UI非表示
                await _uiEffect.FadeOut(windowCanvasGroup, cts.Token);
                await _uiEffect.SizeDelta(windowRectTransform, new Vector2(_initialUISizeDelta.x, setDeltaMinY),
                    setDeltaDuration, cts.Token);
                await _uiEffect.SizeDelta(windowRectTransform,
                    new Vector2(setDeltaMinx, setDeltaMinY), setDeltaDuration, cts.Token);

                // UI無効
                SetActive(false);
            }
            catch (OperationCanceledException)
            {
                // キャンセルされた場合の処理
                Debug.Log("Fade Canceled");
            }
        }
    }
}
using Cysharp.Threading.Tasks;
using Event.Signal;
using UniRx;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace _RAYSER.Scripts.UI.Title
{
    [RequireComponent(typeof(Button))]
    public class ButtonCustomize : MonoBehaviour
    {
        /// <summary>
        /// 対象ボタン
        /// </summary>
        [SerializeField] private Button _button;

        /// <summary>
        /// 閉じるボタン
        /// </summary>
        [SerializeField] private Button _closeButton;

        /// <summary>
        /// メニューUI
        /// </summary>
        [SerializeField] private TitleMenuButtonsUI titleMenuButtonsUI;

        /// <summary>
        /// ウィンドウUI
        /// </summary>
        [SerializeField] private CustomizeWindowUI customizeWindowUI;

        /// <summary>
        /// ゲームパッドのキャンセルボタン受付
        /// </summary>
        private bool _gamePadCancelButtonAcceptance = false;

        private void Reset()
        {
            _button = GetComponent<Button>();
        }

        private void Awake()
        {
            _button.OnClickAsObservable().Subscribe(
                _ => { PushButton(); }
            ).AddTo(this);
            _closeButton.OnClickAsObservable().Subscribe(
                _ => { PushCloseButton(); }
            ).AddTo(this);
        }

        private void Start()
        {
            // ゲームパッドキャンセルボタン受付
            MessageBroker.Default.Receive<GamePadCancelButtonPush>()
                .Where(_ => _gamePadCancelButtonAcceptance)
                .Subscribe(_ => PushCloseButton())
                .AddTo(this);

        }

        private async UniTask PushButton()
        {
            //初期選択ボタンの初期化
            EventSystem.current.SetSelectedGameObject(null);

            await titleMenuButtonsUI.HideUI();
            await customizeWindowUI.ShowUI();
        }

        private async UniTask PushCloseButton()
        {
            // ゲームパッドのキャンセルボタン受付解除
            _gamePadCancelButtonAcceptance = false;

            await customizeWindowUI.HideUI();
            await titleMenuButtonsUI.ShowUI();
        }
    }
}

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