using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum PopupType { Alert, Prompt } public class Popup : MonoBehaviour { public static Action OnPopupClosed; public void Close() { OnPopupClosed(null, gameObject); DestroyImmediate(gameObject); } public void CloseWithBool(bool value) { OnPopupClosed(value, gameObject); DestroyImmediate(gameObject); } public void CloseWithString(string value) { OnPopupClosed(value, gameObject); DestroyImmediate(gameObject); } public void CloseWithGameObject(GameObject value) { OnPopupClosed(value, gameObject); DestroyImmediate(gameObject); } public static void Open(PopupType type, Transform ui, string id, params object[] args) { var popup = Instantiate(Resources.Load("Popup"), Vector2.zero, Quaternion.identity, ui) as GameObject; popup.transform.localPosition = Vector2.zero; if (type == PopupType.Alert) { for (int i = 1; i < popup.transform.childCount; i++) { var child = popup.transform.GetChild(i); child.gameObject.SetActive(child.name == "Alert"); } var alert = popup.transform.Find("Alert"); alert.transform.Find("Title").GetComponent().text = (string)args[0]; alert.transform.Find("Content").GetComponent().text = (string)args[1]; }else if (type == PopupType.Prompt) { for (int i = 1; i < popup.transform.childCount; i++) { var child = popup.transform.GetChild(i); child.gameObject.SetActive(child.name == "Prompt"); } var prompt = popup.transform.Find("Prompt"); popup.GetComponent().sizeDelta = prompt.GetComponent().sizeDelta; prompt.transform.Find("Title").GetComponent().text = (string)args[0]; prompt.transform.Find("Input").GetComponentsInChildren()[0].text = (string)args[1]; } popup.name = id; } }