123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using UnityEngine;
- using System.Collections;
- public class PopUpManager : MonoBehaviour {
- public static bool modal = false;
- public static void UpdateModal()
- {
- PopUpPanel[] popUpPanels = GameObject.FindObjectsOfType<PopUpPanel>();
- for(int i=0; i<popUpPanels.Length; i++)
- {
- PopUpPanel panel = popUpPanels[i];
- if(panel.modal == true && panel.gameObject.activeSelf == true)
- {
- modal = true;
- return;
- }
- }
- modal = false;
- }
- public static PopUpPanel AddPopUp(GameObject panelPrefab, Transform parent=null, bool modal=false, System.Type panelType=null)
- {
- if(panelPrefab == null)
- return null;
- GameObject gameObj = Instantiate(panelPrefab) as GameObject;
- PopUpPanel panel = null;
- if (panelType != null)
- {
- panel = (PopUpPanel)gameObj.AddComponent(panelType);
- }
- else
- {
- panel = gameObj.GetComponent<PopUpPanel>();
- }
- panel.gameObject.AddComponent<PopUpUtil>();
- PopUpManager.AddToMainCanvas(gameObj, parent);
- panel.modal = modal;
- UpdateModal();
- return panel;
- }
- public static T AddPopUp<T>(GameObject panelPrefab, Transform parent=null, bool modal=false) where T : PopUpPanel
- {
- if(panelPrefab == null)
- return null;
- GameObject gameObj = Instantiate(panelPrefab) as GameObject;
- T panel = gameObj.GetComponent<T>();
- if (panel == null)
- {
- panel = gameObj.AddComponent<T>();
- }
- if(panel.GetComponent<PopUpUtil>() == null)
- panel.gameObject.AddComponent<PopUpUtil>();
-
- PopUpManager.AddToMainCanvas(gameObj, parent);
- panel.modal = modal;
- UpdateModal();
- return panel;
- }
- public static void AddToMainCanvas(GameObject gameObj, Transform parent = null)
- {
- RectTransform rectTrans = gameObj.GetComponent<RectTransform>();
- if(rectTrans != null)
- {
- Vector3 originPos = rectTrans.localPosition;
- Vector3 originSizeDelta = rectTrans.sizeDelta;
- if(parent == null)
- {
- Canvas canvas = GameObject.FindObjectOfType<Canvas>();
- parent = canvas.transform;
- }
- gameObj.transform.SetParent(parent);
- gameObj.transform.localScale = new Vector3(1f, 1f, 1f);
- rectTrans.localPosition = originPos;
- rectTrans.sizeDelta = originSizeDelta;
- }
- else
- {
- Vector3 pos = gameObj.transform.localPosition;
- Vector3 scale = gameObj.transform.localScale;
- if(parent == null)
- {
- MainCanvas canvas = GameObject.FindObjectOfType<MainCanvas>();
- parent = canvas.transform;
- }
- gameObj.transform.SetParent(parent);
- gameObj.transform.localScale = scale;
- gameObj.transform.localPosition = pos;
- }
- }
- public static void RemovePopUp(PopUpPanel panel)
- {
- Destroy(panel.gameObject);
- UpdateModal();
- }
- }
|