[TIL] 63 UIManager โญโญโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,


UIManager



์ตœ์ข… ํŒ€ ํ”„๋กœ์ ํŠธ 8์ผ์ฐจ

ย ย  [o] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ - 53
ย ย  [o] ๋ฉด์ ‘ ๋ฌธ์ œ ํ’€๊ธฐ - 5
ย ย  [o] ๋‹ค๋ฅธ๋ฐ˜ ๊ฐ•์˜ ๋“ฃ๊ธฐ ์Šคํƒ ๋‹ค๋“œ2 ์ฑŒ~
ย ย  [x] ์‹ฌํ™”์ฃผ์ฐจ ๊ฐ•์˜ ๋“ฃ๊ธฐ.
ย ย  [x] ๋””์ž์ธ ์ฝ”๋“œ ํŒจํ„ด ์ดํ•ด,์ •๋ฆฌํ•˜๊ธฐ.
ย ย  [x] ์ž๋ฃŒ๊ตฌ์กฐ ๋””์ž์ธํŒจํ„ด ๊ฐ•์˜ ๋‹ค์‹œ ๋“ฃ๊ธฐ.







UI ์ž‘์—… ์ค‘

์‹๋‹น

์‹๋‹น๋ฉ”๋‰ด ์„ ํƒ
image



ํƒ€์ดํ‹€ ์”ฌ

image







UIManager


using System.Collections.Generic;
using UnityEngine;

public class UIManager
{

    private int _canvasSortOrder = 5;
    private Stack<GameObject> _popupStack = new Stack<GameObject>();
    public Dictionary<string, GameObject> _popupDic = new Dictionary<string, GameObject>();

    public void CreateCanvas() 
    {
        GameObject uiObject = GameObject.Find("Uis");
        if (uiObject == null)
        {
            uiObject = new GameObject("Uis");
        
            var pre = Resources.LoadAll<GameObject>("UI/Canvas");
            foreach (var p in pre) 
            {
                _popupDic.Add(p.name, Object.Instantiate(p,uiObject.transform));
                _popupDic[p.name].SetActive(false);
            }
        }
    }

    public void ShowCanvas(string uiname)
    {
        _popupDic[uiname].GetComponent<Canvas>().sortingOrder = _canvasSortOrder;
        _popupStack.Push(_popupDic[uiname]);
        _popupDic[uiname].SetActive(true);
        _canvasSortOrder++;
        Cursor.lockState = CursorLockMode.None;
    }

    public void CloseLastCanvas()
    {
        if (_popupStack.Count == 1)
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        if (_popupStack.Count == 0) 
        {
            ShowCanvas("SettingUI");
        }
        else 
        { 
            GameObject currentUi = _popupStack.Pop();
            currentUi.SetActive(false);
            currentUi = null;
            _canvasSortOrder--;
        }
    }
}

  • CreateCanvas() : ๋ชจ๋“  UI Instantiate
  • Popup(string uiname) : ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์€ UI SetActive, ์ปค์„œ๋ฝ ํ•ด์ œ ๋‹ค์Œui CanvasSortOrder ์ฆ๊ฐ€
  • CloseLastPopup() : ์•„๋ฌดui๋„ ์—†์œผ๋ฉด ์„ธํŒ…์ฐฝ, ์žˆ์œผ๋ฉด ๋‹ซ๊ธฐ,
if (Input.GetKeyDown(KeyCode.Escape))
{
    Debug.Log("esc");
    GameManager.instance.UIManager.CloseLastPopup();
}

// ์ƒํ˜ธ์ž‘์šฉ ์‹œ ~~ ํ…Œ์ŠคํŠธ R ํด๋ฆญ ์‹œ
if (Input.GetKeyDown(KeyCode.R))
{
    GameManager.instance.UIManager.Popup("ShopUI");
}

์žก๋‹ด,์ •๋ฆฌ

ctrl \ + t -> todo์ฃผ์„์ฐพ๊ธฐ
UI๋กœ ์บ”๋ฒ„์Šค๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒŒ ๋งž๋Š”๊ฑธ๊นŒ? ์บ”๋ฒ„์Šค์•ˆ์˜ ๊ฒƒ์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ๊ฒŒ ๋งž๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด๊ธฐ.




์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP




๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ