내일배움캠프 76일차 TIL : 화면 조절, 새게임 버튼

woollim·2025년 1월 10일
0

내일배움캠프TIL

목록 보기
69/74
post-thumbnail

■ 오늘계획

○ 할 일 목록

  • 화면 크기 조절


■ 화면조절

○ UISettingPopup_Screen

  • 전체창, 윈도우창 선택 버튼 추가
  • 드롭다운으로 해상도 설정 가능



○ 코드 전문

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;

public class UISettingPopup_Screen : UIPopUp
{
    [Header("드롭다운")]
    [Tooltip("화면 해상도를 선택할 수 있는 드롭다운 입니다.")]
    [SerializeField] private TMP_Dropdown dropdownScreenResolution;

    [SerializeField] private TMP_Text dropDownResult;

    [Header("버튼")]
    [Tooltip("화면을 전체 창으로 바꾸는 버튼입니다.")]
    [SerializeField] private Button fullScreenButton;

    [Tooltip("화면을 전체 윈도우 창으로 바꾸는 버튼입니다.")]
    [SerializeField] private Button windoeScreenButton;

    [Tooltip("이전 화면으로 돌아가는 버튼 입니다.")]
    [SerializeField] private Button backButton;
    // Start is called before the first frame update
    #region Unity Methods
    /// <summary>
    /// 컴포넌트 초기화 및 이벤트 리스너를 등록합니다.
    /// </summary>
    protected override void Awake()
    {
        base.Awake();

        // 드롭다운 이벤트 연결
        dropdownScreenResolution.onValueChanged.AddListener(delegate { OnDropdownScreenResolution(dropdownScreenResolution); });

        // 버튼 이벤트 연결
        fullScreenButton.onClick.AddListener(OnFullScreenButton);
        windoeScreenButton.onClick.AddListener(OnWindoeScreenButton);
        backButton.onClick.AddListener(OnBackButton);
    }
    #endregion

    /// <summary>
    /// UI가 활성화될 때 현재 볼륨 설정값으로 UI를 초기화합니다.
    /// </summary>
    public override void Opened(object[] param)
    {
        // 게임 시간 멈춤
        Managers.Time.PauseTime();
    }

    /// <summary>
    /// 설정 팝업을 숨기고 UI 매니저에서 제거합니다.
    /// </summary>
    public override void Hide()
    {
        Managers.UI.Hide<UISettingPopup_Screen>();
        Managers.Time.ResumeTime();
    }

    private void OnDisable()
    {
        Managers.Time.ResumeTime();
    }

    private void OnDropdownScreenResolution()
    {
        string screenResolution = dropdownScreenResolution.options[dropdownScreenResolution.value].text;
        dropDownResult.text = screenResolution;
    }

    private void OnDropdownScreenResolution(TMP_Dropdown select)
    {
        string screenResolution = dropdownScreenResolution.options[select.value].text;
        dropDownResult.text = screenResolution;

        string[] parts = screenResolution.Split('*', 2);
        int front = Convert.ToInt32(parts[0]);
        int back = Convert.ToInt32(parts[1]);

        Screen.SetResolution(front, back, Screen.fullScreen);
    }

    private void OnFullScreenButton()
    {
        Managers.Sound.PlaySFX(SFX.UI_Click);
        Screen.SetResolution(1920, 1080, FullScreenMode.FullScreenWindow);
    }

    private void OnWindoeScreenButton()
    {
        Managers.Sound.PlaySFX(SFX.UI_Click);
        Screen.SetResolution(1920, 1080, FullScreenMode.Windowed);
    }

    private void OnBackButton()
    {
        Managers.Sound.PlaySFX(SFX.UI_Click);
        Hide();
        Managers.UI.Show<UISettingPopup>();
    }

    /// <summary>
    /// 종료 버튼 클릭 시 호출되는 이벤트 핸들러입니다.
    /// 에디터에서는 플레이 모드를 종료하고, 빌드에서는 게임을 종료합니다.
    /// </summary>
    private void OnQuitButtonClicked()
    {
        Managers.SaveLoad.SavePlayerDataBase();
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
}

○ 전체창, 윈도우창 버튼


○ 해상도 드롭다운




■ 새게임

  • Continue Button
    • 저장 데이터가 있으면 '컨티뉴' 버튼 비활성화
    • 있으면 활성화
  • New Game
    • 클릭시 저장 데이터가 있으면 기존 데이터 삭제

○ LoginTitleManager

    private void Start()
    {
        // 가이드 UI는 처음에 비활성화
        LoginUI.gameObject.SetActive(false);

        // 타이틀 UI 초기화 및 이벤트 연결
        titleUI.OnLoginRequested += ShowLogin;
        titleUI.OnGameStartRequested += StartGame;

        // 배경 음악 재생
        audioPlayer.SetVolume(0.02f);
        audioPlayer.PlayTitleBGM();
    }

private void StartGame()
{
    if (!startCheck)
    {
        // 저장된 데이터가 있으면 바로 시작
        if (Managers.SaveLoad.SaveFileExists())
        {
            audioPlayer.StopTitleBGM();
            Managers.Scene.LoadScene(ESceneName.Village, () =>
            {
                Managers.Character.Init();
                Managers.Game.Init();
            });
            startCheck = true;
            return;
        }
 }

○ UITitle

    public event System.Action OnLoginRequested;
    public event System.Action OnGameStartRequested;
    
    private void OnContinueButtonClick()
    {
        DisableDoubleClick();

        Managers.Sound.PlaySFX(SFX.UI_ButtonClick);

        if (Managers.SaveLoad.SaveFileExists())
        {
            mainMenuGroup.DOFade(0f, fadeOutDuration)
                .OnComplete(() =>
                {
                    OnGameStartRequested?.Invoke();
                    // 페이드 아웃 완료 후 버튼 다시 활성화
                    ResetButtonState();
                });
        }
    }

    private void OnNewGameButtonClick()
    {
        DisableDoubleClick();

        Managers.Sound.PlaySFX(SFX.UI_ButtonClick);

        mainMenuGroup.DOFade(0f, fadeOutDuration)
                .OnComplete(() =>
                {
                    Managers.SaveLoad.DeleteAllSaveFile(); // 저장데이터가 있다면 삭제
                    OnLoginRequested?.Invoke();
                    // 페이드 아웃 완료 후 버튼 다시 활성화
                    ResetButtonState();
                });

    }

0개의 댓글

관련 채용 정보