using UnityEngine; using System.Collections; public class PopUpManager : MonoBehaviour { public static bool modal = false; public static void UpdateModal() { PopUpPanel[] popUpPanels = GameObject.FindObjectsOfType(); for(int i=0; i(); } panel.gameObject.AddComponent(); PopUpManager.AddToMainCanvas(gameObj, parent); panel.modal = modal; UpdateModal(); return panel; } public static T AddPopUp(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(); if (panel == null) { panel = gameObj.AddComponent(); } if(panel.GetComponent() == null) panel.gameObject.AddComponent(); PopUpManager.AddToMainCanvas(gameObj, parent); panel.modal = modal; UpdateModal(); return panel; } public static void AddToMainCanvas(GameObject gameObj, Transform parent = null) { RectTransform rectTrans = gameObj.GetComponent(); if(rectTrans != null) { Vector3 originPos = rectTrans.localPosition; Vector3 originSizeDelta = rectTrans.sizeDelta; if(parent == null) { Canvas canvas = GameObject.FindObjectOfType(); 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(); 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(); } }