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.
 
 
 
 
 

58 lines
2.8 KiB

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<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, 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>().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];
}
popup.name = id;
}
}