Programming/Unity
[Unity] 유니티 팝업 윈도우 - Popup
타임박스
2019. 8. 27. 18:01
반응형
✔ Unity에서 알림창, 팝업창을 만들어보자!
프로젝트 진행 시 매번 팝업 창을 만들기 번거로워서 패키지로 만들기 위한 목적으로 제작함..
모드는 2가지 형태로 특정 내용만 보이게 하는 방법과 확인버튼으로 특정 이벤트를 실행하도록 만들었다.
● 소스코드 내용
using UnityEngine;
using UnityEngine.UI;
using System;
public class PopupCheckConfirmEventArgs : EventArgs
{
public UnityEngine.Events.UnityAction events;
public string eventName;
}
public class PopupWindow : MonoBehaviour {
public static PopupWindow instance;
public static event EventHandler<PopupCheckConfirmEventArgs> ConfirmEvent;
public enum MsgType { notice, error, warning, exit };
public GameObject popupView;
public Text m_MessageType;
public Text m_Msg;
public Button Btn_Exit;
public Button Btn_OK;
public Button Btn_Cancle;
// Use this for initialization
void Start ()
{
if(instance == null)
instance = this;
Btn_Exit.onClick.AddListener(CloseView);
Btn_Cancle.onClick.AddListener(CloseView);
ConfirmEvent += OnConfirmOnClick;
if (transform.parent != null && transform.parent.GetComponent<Canvas>() != null)
transform.SetSiblingIndex(transform.parent.childCount);
else
Debug.LogError("Popup Window 패키지가 Canvas 자식으로 설정되어있지 않습니다. Canvas 하위 자식으로 설정하세요.");
}
public void CloseView()
{
popupView.SetActive(false);
Btn_OK.gameObject.SetActive(false);
Btn_Cancle.gameObject.SetActive(false);
Btn_OK.onClick.RemoveAllListeners();
}
public void PopupCheckWindowOpen(UnityEngine.Events.UnityAction action, string actionName, MsgType msgtype, string msg)
{
Btn_OK.gameObject.SetActive(true);
Btn_Cancle.gameObject.SetActive(true);
Btn_OK.onClick.AddListener(delegate { ConfirmEvent(this, new PopupCheckConfirmEventArgs() { events = action, eventName = actionName }); });
PopupWindowOpen(msgtype, msg);
}
public void OnConfirmOnClick(object sender, PopupCheckConfirmEventArgs e)
{
e.events();
Btn_OK.onClick.RemoveAllListeners();
CloseView();
}
public void PopupWindowOpen(MsgType msgtype, string msg)
{
popupView.SetActive(true);
if (msgtype == MsgType.error)
{
m_MessageType.text = "Error !";
m_Msg.text = msg;
m_MessageType.color = Color.red;
}
else if (msgtype == MsgType.warning)
{
m_MessageType.text = "Warning !";
m_Msg.text = msg;
m_MessageType.color = Color.yellow;
}
else if (msgtype == MsgType.exit)
{
m_MessageType.text = "EXIT";
m_Msg.text = msg;
m_MessageType.color = Color.white;
}
else
{
m_MessageType.text = "Notice";
m_Msg.text = msg;
m_MessageType.color = Color.white;
}
}
}
다운로드 :
PopupWindow.unitypackage
0.02MB
CloseView : 팝업 윈도우를 닫을 때 사용됩니다.
PopupCheckWindowOpen : OK 버튼과 Cancle 버튼으로 이루어진 팝업이 나타납니다.
OnComfirmOnClick : OK 버튼이 눌러졌을때 이벤트가 콜되는 함수입니다. PopupCheckWindowOpen에서 등록된 이벤트가 실행됨.
PopupWindowOpen : 단순 Text 확인용 팝업을 띄울때 사용됩니다.
● 활용 예제 (1) - 단순 메세지 알림용으로만 사용될때
PopupWindow.instance.PopupWindowOpen(PopupWindow.MsgType.notice, "로그인 되었습니다.");
● 활용 예제2 - 확인, 취소 버튼으로 확인 버튼 클릭했을때 특정 로직을 수행해야할때
void Start()
{
//처리 함수 생성
void action()
{
Application.Quit();
}
PopupWindow.instance.PopupCheckWindowOpen(action, "Exit", PopupWindow.MsgType.warning, "프로그램을 종료하시겠습니까?");
}
감사합니다.
반응형