using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum PopupType { Alert, Prompt, List } 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, Func func, params dynamic[] 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.FindRecursive("AlertText").GetComponent().text = (string)args[1]; TextGenerator textGen = new TextGenerator(); TextGenerationSettings textGenSettings = alert.transform.FindRecursive("AlertText").GetComponent().GetGenerationSettings(alert.transform.FindRecursive("AlertText").GetComponent().rect.size); var height = textGen.GetPreferredHeight((string)args[1], textGenSettings); alert.transform.FindRecursive("AlertText").GetComponent().sizeDelta = new Vector2(alert.transform.FindRecursive("AlertText").GetComponent().sizeDelta.x, alert.transform.FindRecursive("AlertText").GetComponent().preferredHeight); } 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]; } else if (type == PopupType.List) { for (int i = 1; i < popup.transform.childCount; i++) { var child = popup.transform.GetChild(i); child.gameObject.SetActive(child.name == "List"); } var list = popup.transform.Find("List"); popup.GetComponent().sizeDelta = list.GetComponent().sizeDelta; list.transform.Find("Title").GetComponent().text = (string)args[0]; var scrollContent = list.transform.FindRecursive("ScrollContent"); var baseElement = Instantiate((GameObject)args[1], Vector3.zero, Quaternion.identity, scrollContent); baseElement.transform.SetAsFirstSibling(); baseElement.transform.localScale = Vector3.one; baseElement.SetActive(false); for (int i = 0; i < args[2].Length; i++) { var el = Instantiate((GameObject)args[1], Vector3.zero, Quaternion.identity, scrollContent); el.transform.SetParent(scrollContent); el.transform.SetAsLastSibling(); el.GetComponent().Init(popup, 3, func(args[2][i])); } } popup.name = id; } public static void Open(PopupType type, Transform ui, string id, params object[] args) { Popup.Open(type, ui, id, null, args); } }