[TIL] 63 UIManager โญโญโญ
์นดํ ๊ณ ๋ฆฌ: Til
UIManager
์ต์ข ํ ํ๋ก์ ํธ 8์ผ์ฐจ
ย ย [o] ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ - 53
ย ย [o] ๋ฉด์ ๋ฌธ์ ํ๊ธฐ - 5
ย ย [o] ๋ค๋ฅธ๋ฐ ๊ฐ์ ๋ฃ๊ธฐ ์คํ ๋ค๋2 ์ฑ~
ย ย [x] ์ฌํ์ฃผ์ฐจ ๊ฐ์ ๋ฃ๊ธฐ.
ย ย [x] ๋์์ธ ์ฝ๋ ํจํด ์ดํด,์ ๋ฆฌํ๊ธฐ.
ย ย [x] ์๋ฃ๊ตฌ์กฐ ๋์์ธํจํด ๊ฐ์ ๋ค์ ๋ฃ๊ธฐ.
UI ์์ ์ค
์๋น
์๋น๋ฉ๋ด ์ ํ
ํ์ดํ ์ฌ
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๋ก ์บ๋ฒ์ค๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ ๋ง๋๊ฑธ๊น? ์บ๋ฒ์ค์์ ๊ฒ์ ๋ณ์์ ์ ์ฅํ๋ ๊ฒ ๋ง๋์ง ์๊ฐํด๋ณด๊ธฐ.
๋๊ธ๋จ๊ธฐ๊ธฐ