Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

booooox

[Unity] PopupManager (팝업 시스템) 본문

Unity/System

[Unity] PopupManager (팝업 시스템)

booooox 2024. 10. 9. 18:55
반응형

1. PopupManager : Popup 클래스를 상속하고 있는 클래스를 호출 및 해제 관리 해주는 시스템

 

2. 기대효과 :

    1) Popup UI들을 효과적으로 관리 제어 할 수 있다.

    2) 한개의 해당 Popup을 가지고 있을 수 있다.

    3) 오직 Popup 클래스만 호출 가능 하여 버그를 방지할 수 있다.

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class PopupManager : MonoSingleton<PopupManager>
{
    [field : SerializeField] public Canvas Canvas_Popup { get; private set; }
    [SerializeField] private SerializedDictionary<string, Popup> popupDictionary = new SerializedDictionary<string, Popup>();
    private string popupResourcesPath = "Prefabs/UI/Popup/";

    /// <summary>
    /// 알림 팝업 (크기 가변)
    /// </summary>
    public NoticePopup Popup(string msgText)
    {
        return Popup("", msgText, null, null, "", "");
    }
    public NoticePopup Popup(string titleText, string msgText)
    {
        return Popup(titleText, msgText, null, null, "", "");
    }
    public NoticePopup Popup(string msgText, Action okAction)
    {
        return Popup("", msgText, okAction, null, "", "");
    }
    public NoticePopup Popup(string titleText, string msgText, Action okAction)
    {
        return Popup(titleText, msgText, okAction, null, "", "");
    }
    public NoticePopup Popup(string titleText, string msgText, Action okAction, string okText)
    {
        return Popup(titleText, msgText, okAction, null, okText, "");
    }
    public NoticePopup Popup(string titleText, string msgText, Action okAction, Action calnelAction, string okText, string cancelText)
    {
        NoticePopup popup = Open<NoticePopup>();

        if (okAction == null) popup.RemoveCancelButton();
        else popup.ShowCancelButton();

        okAction += () => Close(popup);
        calnelAction += () => Close(popup);

        popup.Setting(titleText, msgText, okAction, calnelAction, okText, cancelText);
        return popup;
    }

    /// <summary>
    /// 팝업 클래스로 호출 시 ex) PopupManager.instance.Open<Popup>();
    /// </summary>
    public T Open<T>() where T : Popup
    {
        string key = typeof(T).ToString();

        if (popupDictionary.TryGetValue(key, out Popup value)) return (T)value;

        value = Instantiate(Resources.Load<Popup>(popupResourcesPath + key), Canvas_Popup.transform, false);

        popupDictionary.Add(key, value);

        return (T)value;
    }

    /// <summary>
    /// 팝업 호출 시 ex) PopupManager.instance.Open(Popup);
    /// </summary>
    public T Open<T>(T popup) where T : Popup
    {
        if (popup == null) return null;

        string key = popup.GetType().ToString();

        if (popupDictionary.TryGetValue(key, out Popup value)) return (T)value;

        value = Instantiate(Resources.Load<Popup>(popupResourcesPath + key), Canvas_Popup.transform, false);

        popupDictionary.Add(key, value);

        return (T)value;
    }

    /// <summary>
    /// 팝업 클래스로 호출 시 ex) PopupManager.instance.Close<Popup>();
    /// </summary>
    public void Close<T>(bool immediately = false) where T : Popup
    {
        string key = typeof(T).ToString();

        if (!popupDictionary.TryGetValue(key, out Popup value)) return;

        popupDictionary.Remove(key);

        value.ClosePopup(immediately : immediately);
    }

    /// <summary>
    /// 팝업 오브젝트로 호출 시 ex) PopupManager.instance.Close(Popup);
    /// </summary>
    public void Close<T>(T popup, bool immediately = false) where T : Popup
    {
        string key = popup.GetType().ToString();

        if (!popupDictionary.TryGetValue(key, out Popup value)) return;

        popupDictionary.Remove(key);

        value.ClosePopup(immediately : immediately);
    }
}

 

3. 사용법 : 

    1)  클래스로 호출 시 <> 안에 Popup이 상속된 클래스를 입력

    2) 오브젝트로 호출 시 () 안에 Popup이 상속된 오브젝트 입력

public class System : MonoBehaviour
{
    // 프리팹
    public GameObejct SettingPopupPoupPrefab;

    // Open (클래스로 호출 시)
    PopupManager.instance.Open<SettingPopup>();
    // Open (오브젝트로 호출 시)
    PopupManager.instance.Open(SettingPopupPoupPrefab);
    
    // Close (클래스로 호출 시)
    PopupManager.instance.Close<SettingPopup>();
    // Close (오브젝트로 호출 시)
    PopupManager.instance.Close(SettingPopupPoupPrefab);
    
    // Notice 팝업
    PopupManager.instance.Popup("Title", "메세지, () => Debug.Log("OkAction"), () => Debug.Log("CancelAction"))
}

 

4. 주의점 :

    1) 호출할 클래스는 Popup 클래스를 가지고 있어야 한다.

    2) 해당 프리팹은 Resources 폴더에 있어야 한다. (popupResourcesPath 위치)

 

MonoSingleton 은 https://booooox.tistory.com/2 에서 확인 가능

 

반응형