Unity : Pop Up 기능
오늘 선발대 세션이 있었는데 UI 프로그래밍에 대해 다뤘다. 그 중에서 팝업창을 띄우는 것에 대한 내용이 가장 중요하게 들렸다.
아래는 팝업과 관련된 기능이 있는 UIManager 클래스의 코드이다. 세션에서 보여주신 코드에는 더 많은 내용이 있었는데 그 중에 간단한 부분만 정리해서 올려주셨다.
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour
{
private static UIManager _singleton = new UIManager();
public static UIManager Get() { return _singleton; }
public static bool Has() { return _singleton != null; }
private List<UIPopup> popups = new List<UIPopup>();
public UIPopup ShowPopup(string popupname)
{
var obj = Resources.Load("Popups/" + popupname, typeof(GameObject)) as GameObject;
if (!obj)
{
Debug.LogWarning("Failed to ShowPopup({0})".SFormat(popupname));
return null;
}
return ShowPopupWithPrefab(obj, popupname);
}
public T ShowPopup<T>() where T : UIPopup
{
return ShowPopup(typeof(T).Name) as T;
}
public UIPopup ShowPopupWithPrefab(GameObject prefab, string popupName)
{
var obj = Instantiate(prefab);
return ShowPopup(obj, popupName);
}
public UIPopup ShowPopup(GameObject obj, string popupname)
{
var popup = obj.GetComponent<UIPopup>();
popups.Insert(0, popup);
obj.SetActive(true);
//obj.transform.SetParent(??, false);
return popup;
}
}
팝업창을 앞으로도 쓸 일이 많을 것 같아서 숙달 해야겠다.