🐧 λ“€μ–΄κ°€κΈ° μ•žμ„œ

UIλ₯Ό μ œμž‘ν•˜λ‹€λ³΄λ‹ˆ νŒμ—…μ΄ ν•„μš”ν–ˆλ‹€.

κ²Œμž„μ„ μ’…λ£Œν•  λ•Œ 정말 μ’…λ£Œν•  것인지 λ¬»λŠ” 쀑간 확인 단계가 ν•„μš”ν–ˆλ‹€.

λ”°λΌμ„œ Popup을 λ§Œλ“€μ—ˆλŠ”λ°,

ν”„λ¦¬νŒΉμœΌλ‘œ λ§Œλ“€μ–΄λ‘κ³ , ν…μŠ€νŠΈλŠ” μ›ν•˜λŠ” 상황에 따라 κ΅μ²΄ν•˜κΈ° μœ„ν•΄ μž‘μ—…μ„ μ‹€μ‹œν–ˆλ‹€.


🐧 였늘 배운 것

섀계

ConfirmationPopup.cs

이 ν΄λž˜μŠ€λŠ” μž¬μ‚¬μš© κ°€λŠ₯ν•œ νŒμ—…μ„ μ œμž‘ν•˜κΈ° μœ„ν•΄ λ©”μ‹œμ§€μ™€ μ•‘μ…˜μ„ μ„€μ •ν•΄μ„œ μ‚¬μš©ν–ˆλ‹€.

SetMessage, SetYes/NoAction λ©”μ„œλ“œλ₯Ό μ œμž‘ν•΄ λ‹€μ–‘ν•œ μƒν™©μ—μ„œ νŒμ—…μ˜ λ‚΄μš©μ„ λ³€κ²½ν•  수 있게 μ˜λ„ν–ˆλ‹€.

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

public class ConfirmationPopup : MonoBehaviour
{
    [SerializeField] private TMP_Text messageTxt;
    [SerializeField] private Button yesButton;
    [SerializeField] private Button noButton;

    public void SetMessage(string message)
    {
        messageTxt.text = message;
    }

    public void SetYesAction(Action yesAction)
    {
        yesButton.onClick.RemoveAllListeners();
        yesButton.onClick.AddListener(new UnityEngine.Events.UnityAction(yesAction));
    }

    public void SetNoAction(Action noAction)
    {
        noButton.onClick.RemoveAllListeners();
        noButton.onClick.AddListener(new UnityEngine.Events.UnityAction(noAction));
    }

    public void ClosePopup()
    {
        gameObject.SetActive(false);
    }
}

PopupTexts.cs

νŒμ—…μ— ν‘œμ‹œλ  ν…μŠ€νŠΈλ₯Ό κ΄€λ¦¬ν•˜λŠ” ν΄λž˜μŠ€λ‹€. λ¬Έμžμ—΄ ν‚€λ₯Ό μ΄μš©ν•΄ ν…μŠ€νŠΈλ₯Ό μ €μž₯ν•˜κ³  κ²€μƒ‰ν•˜λŠ” 방식이닀.

μΆ”ν›„μ—λŠ” Localizationκ³Ό 연동해 μ–Έμ–΄κ°€ 변경될 λ•Œ κ΄€λ¦¬ν•˜λŠ” λ°©μ‹μœΌλ‘œ λ³€κ²½ν•΄μ•Όν•œλ‹€.

using System.Collections.Generic;

public class PopupTexts
{
    private Dictionary<string, string> popupTxts = new Dictionary<string, string>();

    public PopupTexts()
    {
        popupTxts.Add("exitConfirmation", "정말 λ‚˜κ°€μ‹œκ² μŠ΅λ‹ˆκΉŒ? ν˜„μž¬ μ§„ν–‰ν•œ 데이터λ₯Ό λͺ¨λ‘ μžƒμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.");
    }

    public string GetText(string key)
    {
        if(popupTxts.TryGetValue(key, out string value))
        {
            return value;
        }
        return "ν…μŠ€νŠΈ μ—†μŒ";
    }
}

UIManager.cs

νŒμ—… ν”„λ¦¬νŒΉμ„ μƒμ„±ν•˜λŠ” μ½”λ“œλ₯Ό μΆ”κ°€ν–ˆλ‹€.

기쑴에 μ‚¬μš©ν•˜λ˜ UIManagerμ—μ„œ κΈ°λŠ₯을 μΆ”κ°€ν–ˆλ‹€.

public void OpenConfirmationPopup(string message, Action yesAction)
{
    ConfirmationPopup popup = OpenUI<ConfirmationPopup>();
    popup.SetMessage(message);
    popup.SetYesAction(yesAction);
    popup.SetNoAction(popup.ClosePopup);
}

적용 μ½”λ“œ

UIνŒμ—…μ„ 뢈러였고, ν‚€λ₯Ό ν˜ΈμΆœν•΄ ν•΄λ‹Ήν•˜λŠ” ν…μŠ€νŠΈλ₯Ό κ°€μ Έμ˜¬ 수 μžˆλ„λ‘ ν–ˆλ‹€. λ§Œμ•½, "예" λ²„νŠΌμ„ λˆ„λ₯΄λ©΄, () => λ’€μ˜ ν•¨μˆ˜κ°€ μ‹€ν–‰λœλ‹€.

public void OnClickExitBtn()
{
    PopupTexts popupTexts = new PopupTexts();
    string exitConfirmationTxt = popupTexts.GetText("exitConfirmation");

    UIManager.Instance.OpenConfirmationPopup(
        exitConfirmationTxt,
        () => LoadingSceneController.LoadScene("StartScene")
        );
}

🐧 κ²Œμž„μ— κ΅¬ν˜„ν•œλ‹€λ©΄?

  1. λ‹€λ§Œ κ³ λ―Όλ˜λŠ” 뢀뢄은 μ–Έμ–΄κ°€ 변경될 λ•Œ μ–΄λ–»κ²Œ 처리λ₯Όν• μ§€?

  2. κ°œμ„ μ μ΄ λ³΄μ΄λŠ”λ°, UIPause클래슀(μ™ΈλΆ€ 클래슀)μ—μ„œ PopupTexts의 μƒˆ μΈμŠ€ν„΄μŠ€λ₯Ό 맀번 μƒμ„±ν•˜κ³  μžˆλ‹€.
    μ΄λ•Œ νŒμ—… μƒμ„±μ‹œ ν…μŠ€νŠΈ 데이터λ₯Ό λ‹€μ‹œ λ‘œλ“œν•˜κΈ° λ§Œλ“œλŠ”λ°, 이 뢀뢄을 UIManagerλ‚˜ μ€‘μ•™ν™”λœ λ‹€λ₯Έ μ–΄λ–€ 방법을 생각해봐야겠닀.

  3. μ½”λ“œ μž¬μ‚¬μš© 및 λͺ¨λ“ˆν™”κ°€ ν•„μš”ν•œ 것 κ°™λ‹€. ν˜„μž¬ κ΅¬μ‘°λŠ” νŒμ—…μ΄ λ³„λ„λ‘œ κ΄€λ¦¬λ˜κ³  μžˆλŠ”λ°(νŒμ—… λ‹«κΈ° λ²„νŠΌ, μ• λ‹ˆλ©”μ΄μ…˜) 이 뢀뢄듀을 μΆ”μƒν™”ν•΄μ„œ λͺ¨λ“ˆν™”ν•˜λ©΄ 더 효율적인 νŒμ—… 관리가 될 것 κ°™λ‹€.


🐧 내일 ν•  일

λ‘œμ»¬λΌμ΄μ œμ΄μ…˜ κ³΅λΆ€ν•˜κΈ°.

0개의 λŒ“κΈ€