You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

82 lines
4.1 KiB

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<object, GameObject> 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<dynamic, dynamic[]> 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>().text = (string)args[0];
alert.transform.FindRecursive("AlertText").GetComponent<Text>().text = (string)args[1];
TextGenerator textGen = new TextGenerator();
TextGenerationSettings textGenSettings = alert.transform.FindRecursive("AlertText").GetComponent<Text>().GetGenerationSettings(alert.transform.FindRecursive("AlertText").GetComponent<RectTransform>().rect.size);
var height = textGen.GetPreferredHeight((string)args[1], textGenSettings);
alert.transform.FindRecursive("AlertText").GetComponent<RectTransform>().sizeDelta = new Vector2(alert.transform.FindRecursive("AlertText").GetComponent<RectTransform>().sizeDelta.x, alert.transform.FindRecursive("AlertText").GetComponent<Text>().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<RectTransform>().sizeDelta = prompt.GetComponent<RectTransform>().sizeDelta;
prompt.transform.Find("Title").GetComponent<Text>().text = (string)args[0];
prompt.transform.Find("Input").GetComponentsInChildren<Text>()[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<RectTransform>().sizeDelta = list.GetComponent<RectTransform>().sizeDelta;
list.transform.Find("Title").GetComponent<Text>().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<ListElement>().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);
}
}